Monday 9 April 2012

The Arduino, LEDs and PING sensors

So it's time to really get down to business and begin work with the Arduino. For anyone who hasn't heard of it, it's a board based around the AT Mega chip. It's entirely open source so you can build your own from scratch, however for £20 you can purchase the low end (but still very useful) Arduino Uno board. This has the USB built in along with all the components required to get the AT Mega doing it's job.



Exposed to you are 14 general purpose digital IO pins, some of which have hardware support for pulse width modulation. This is a handy feature that lets you output pulses at a fixed rate to communicate with other devices. Unlike the Basic Stamp however, you don't have to wait for pulse(s) to finish as they are buffered up and generated in hardware. Aside from PWM, all the digital pins can be set as inputs or outputs, and can then be set to be high or low.

The analogue input pins actually read a signal from 0V to 5V and convert it into a number between 0 and 1023.  This is pretty cool but I must confess I've not found much use for it yet - all my sensors have built in analogue to digital conversion. I guess something like a photo transistor would hook up nicely to these. As a side note you can also use 2 of the pins for I2C communication which I'll go into on a later post.




The language you'll be using is a slightly mutated version of c++ in a pretty basic development environment (shown on the left). I have to say if there's a failing with the Arduino it's here - the IDE is extremely basic and compiling is ridiculously slow on my 8 core monster machine at work. Still, it does the job and certainly hasn't put me off. Plus as a profesional programmer I've also probably been a bit spoiled using Visual Studio and Visual Assist all these years :)




At this point I have to make a massive call out to Jeremy Blum for his amazing you tube tutorials which really helped get me started.

Everything on the proto board
So here's my first experiment, having chucked all my remaining fun stuff onto the prototype board I started by turning those LEDs on and off in response to pressing the button - basically the first step in Jeremy's tutorials. This goes very well and I am happy :)



PING sensor circuit
Next up is the PING sensors which was a little more interesting. The circuit diagram here is clearly not too complex, but the code was a tiny bit more fiddly. The sensor requires a 5us pulse into Sig to trigger. Once active, the sensor transmits a burst of ultra sound, and emits a pulse back down Sig when the sound bounces back. The result is that your code needs to:

  • Set the pin to output mode
  • Output HIGH for 5us, then return to LO
  • Set the pin to input mode
  • Time how long it takes for the pin to go HIGH again
  • The distance the sound travelled is then equal to the time taken divided by the speed of sound.
  • Thus the distance to the obstacle is half that - i.e. 0.5 * (time / speed_sound)

In code this looks like:


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


int readUltraSound()
{
  pinMode(ultraSoundPin, OUTPUT);
  digitalWrite(ultraSoundPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ultraSoundPin, LOW);
  pinMode(ultraSoundPin, INPUT);
  
  return microsecondsToCentimeters(pulseIn(ultraSoundPin, HIGH));  
}

Note the pulseIn command on the last line. It's this that waits for the pin to go high and times how long it stays that way in microseconds.

While there I also hooked up the motion sensor, which is very cool and ridiculously simple. You just power it with a circuit just like the one from the PING sensor, it it sends a HIGH signal if movement is detected. Thus the code is quite simply:

int readMotion()
{

  return digitalRead(motionPin);
}


Finally, I hooked up the accelerometer with the simple circuit shown here:

Arduino connected to Mesmic 2125 Dual Axis Accelerometer

This is a simple sensor that outputs pulses which can be read using the IO pins. The length of the pulse determines the acceleration in a certain direction. Due to the way the sensor works this same data can be used to read tilt in a stationary device. However while I hooked it up, I didn't get any further than just printing out the raw data. Hopefully once I have a moving robot I can come back to this and put it to better use.

That just about rounds it up for this step. Aside from testing the sensors and using them to toggle the LEDs I didn't get anything too interesting going. My gut feeling about the Arduino right now.... AWESOME! In the next blog I'll go over my first experiences with the SpeakJet - a microchip devoted to synthezing speech and other sounds.




No comments:

Post a Comment