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 |
PING sensor circuit |
- 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));
}
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