<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
	<title>Arduino - Tutorials</title>
	<link>http://www.foxytronics.com/tutorials/_/arduino/</link>
	<pubDate>Sat, 02 May 2026 16:26:39 +0000</pubDate>
	<ttl>86400</ttl>
	<description></description>
	<item>
		<title>How to make a robot</title>
		<link>http://www.foxytronics.com/tutorials/_/arduino/how-to-make-a-robot-r35</link>
		<description><![CDATA[<span style='font-size: 24px;'>Introduction</span><br />This tutorial shows you how to quickly and easily build your first robot! Robotics is an exciting and fun hobby that has become very affordable in recent years. What would have required a lot of money and experience to do a few decades ago is now affordable, easy, and most of all, FUN!<br /><br />In this tutorial, you will learn step-by-step how to make your first Arduino based robot! Keep in mind that this tutorial shows <strong class='bbc'>just one way</strong> to make a robot -- you're limited only by your budget and imagination. If you want to buy better wheels, or a nicer chassis, go for it! You can still follow this tutorial as a guide! The entire process will take a few hours, so get ready to have some fun!<br /><br /><span style='font-size: 24px;'>Parts</span><br />This tutorial was written with the FoxyTronics <a href='http://www.foxytronics.com/products/234-arduino-starter-robotics-kit' class='bbc_url' title=''>Arduino Starter Robotics Kit</a> in mind, but you can follow along with a different kit just as easily as long as you have similar components. The Arduino Starter Robotics Kit comes with the following:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_98506.jpg' alt='Posted Image'  /></span><br /><br />In the above picture:<br /><br />1 x <a href='http://www.foxytronics.com/products/185-arduino-compatible-uno-r3' class='bbc_url' title=''>Arduino Compatible UNO R3</a><br />2 x <a href='http://www.foxytronics.com/products/68-springrc-sm-s4303r-continuous-rotation-servo' class='bbc_url' title=''>SpringRC SM-S4303R Continuous Rotation Servo</a><br />1 x <a href='http://www.foxytronics.com/products/132-hc-sr04-ultrasonic-distance-sensor' class='bbc_url' title=''>HC-SR04 Ultrasonic Distance Sensor</a><br />1 x <a href='http://www.foxytronics.com/products/182-4-pin-male-to-female-cable' class='bbc_url' title=''>4-Pin Male to Female Cable</a><br />1 x <a href='http://www.foxytronics.com/products/54-small-solderless-breadboard-400-contacts' class='bbc_url' title=''>Small Solderless Breadboard (400 contacts)</a><br />1 x <a href='http://www.foxytronics.com/products/90-breadboard-jumper-wire-bundle-70-pack' class='bbc_url' title=''>Breadboard Jumper Wire Bundle (70-pack)</a><br />1 x <a href='http://www.foxytronics.com/products/52-4-black-nylon-zip-ties' class='bbc_url' title=''>4" Black Nylon Zip-Ties</a><br />1 x <a href='http://www.foxytronics.com/products/80-6-x-aa-battery-holder-with-dc-power-jack-9v' class='bbc_url' title=''>6 x AA Battery Holder with DC Power Jack (9V)</a><br />2 x <a href='http://www.foxytronics.com/products/61-limit-switch' class='bbc_url' title=''>Limit Switch</a><br />1 x <a href='http://www.foxytronics.com/products/1-break-away-headers-single-row-straight' class='bbc_url' title=''>Break Away Headers (Single Row, Straight)</a><br />2 x <a href='http://www.foxytronics.com/products/18-mini-push-button-switch' class='bbc_url' title=''>Mini Push Button Switch</a><br />2 x <a href='http://www.foxytronics.com/products/135-photoresistor' class='bbc_url' title=''>Photoresistor</a><br />5 x <a href='http://www.foxytronics.com/products/2-5mm-red-led-ultra-bright' class='bbc_url' title=''>5mm Red LED (ultra bright)</a><br />5 x <a href='http://www.foxytronics.com/products/31-5mm-green-led-ultra-bright' class='bbc_url' title=''>5mm Green LED (ultra bright)</a><br />10 x <a href='http://www.foxytronics.com/products/145-1-4w-330-ohm-carbon-film-resistor' class='bbc_url' title=''>1/4W 330Ω Carbon Film Resistor</a><br />10 x <a href='http://www.foxytronics.com/products/34-1-4w-1k-ohm-carbon-film-resistor' class='bbc_url' title=''>1/4W 1kΩ Carbon Film Resistor</a><br />16" x <a href='http://www.foxytronics.com/products/233-heavy-duty-double-sided-tape' class='bbc_url' title=''>Heavy Duty Double Sided Tape</a><br />10 x <a href='http://www.foxytronics.com/products/148-1-4w-4-7k-ohm-carbon-film-resistor' class='bbc_url' title=''>1/4W 4.7kΩ Carbon Film Resistor</a><br />10 x <a href='http://www.foxytronics.com/products/149-1-4w-10k-ohm-carbon-film-resistor' class='bbc_url' title=''>1/4W 10kΩ Carbon Film Resistor</a><br /><br />To start, let's go over each item in the kit:<br /><br /><span style='font-size: 18px;'>Arduino Compatible UNO R3</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/185/arduino-compatible-uno-rev3-610.jpg' alt='Posted Image'  /></span><br />The Arduino Uno is the brain of the robot. The most important component on an Arduino is the ATmega328 microcontroller, which is like a mini computer. We will download a program to the microcontroller that will control the robot.<br /><br /><span style='font-size: 18px;'>SpringRC SM-S4303R Continuous Rotation Servo</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/68/large-continuous-rotation-servo-563.jpg' alt='Posted Image'  /></span><br />We'll use two of these continuous rotation servos to make our robot move around. Continuous rotation servos are like normal brushless DC motors, but with built-in speed control. The speed of the motors is controlled through the use of Pulse-Width Modulation (PWM), but we won't have to worry about the specifics since the Arduino takes care of all of that for us.<br /><br /><span style='font-size: 18px;'>HC-SR04 Ultrasonic Distance Sensor</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/132/hc-sr04-ultrasonic-distance-sensor-779.jpg' alt='Posted Image'  /></span><br />Ultrasonic sensors are pretty cool. They work a lot like sonar in a submarine and are used to measure distance. We'll use it on our robot to detect when there's an object in front of us.<br /><br /><span style='font-size: 18px;'>4-Pin Male to Female Cable</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/182/4-pin-male-to-female-cable-602.jpg' alt='Posted Image'  /></span><br />In the tutorial we'll be plugging the ultrasonic sensor into the breadboard, but if you want to mount the sensor somewhere else (higher up, or on a servo for example), then you can use this cable to easily connect it to the breadboard.<br /><br /><span style='font-size: 18px;'>Small Solderless Breadboard (400 contacts)</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/54/small-solderless-breadboard-400-contacts-770.jpg' alt='Posted Image'  /></span><br />A breadboard is a great way to prototype circuits, or to just make things without the hassle of soldering. We'll be using the breadboard for our external circuitry.<br /><br /><span style='font-size: 18px;'>Breadboard Jumper Wire Bundle (70-pack)</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/90/breadboard-jumper-wire-bundle-70-pack-coming-soon-604.jpg' alt='Posted Image'  /></span><br />Breadboard jumper wires are very handy to have. In the tutorial, we used <a href='http://www.foxytronics.com/category/18-wire/' class='bbc_url' title=''>22 gauge hook up wire</a>, but jumper wires are actually easier to use since you don't have to strip the ends.<br /><br /><span style='font-size: 18px;'>4" Black Nylon Zip Ties</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/52/4-black-nylon-zip-ties-715.jpg' alt='Posted Image'  /></span><br />Zip Ties (also called "Cable Ties") are just plain useful. You'll use a lot of these, trust me.<br /><br /><span style='font-size: 18px;'>6 x AA Battery Holder with DC Power Jack (9V)</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/80/6-x-aa-battery-holder-with-dc-power-jack-9v-575.jpg' alt='Posted Image'  /></span><br />The Arduino requires a 9V DC input to operate, so this battery pack works perfectly as it can be plugged right into the Arduino!<br /><br /><span style='font-size: 18px;'>Limit Switch</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/61/limit-switch-668.jpg' alt='Posted Image'  /></span><br />Limit switches come in handy all the time. They can be used for a variety of things, such as bump sensors on your robot.<br /><br /><span style='font-size: 18px;'>Break Away Headers (Single Row, Straight)</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/1/break-away-headers-single-row-straight-762.jpg' alt='Posted Image'  /></span><br />Break away headers are strips of male pins that can be snapped off into segments of any length. We will use two 3-pin segments in this tutorial to connect the servo motors to the breadboard.<br /><br /><span style='font-size: 18px;'>Mini Push Button Switch</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/18/mini-push-button-switch-807.jpg' alt='Posted Image'  /></span><br />These mini push button switches rock! They're tiny, so they don't take up much space on a breadboard, but the button is large and tall enough that it's easy to press. They're also really cheap! Push button switches are a great way to add user input to a project. If you look closely, you'll notice that the Arduino uses a push button as a reset switch.<br /><br /><span style='font-size: 18px;'>Photoresistor</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/135/photoresistor-784.jpg' alt='Posted Image'  /></span><br />Photoresistors are pretty neat. A photoresistor is a resistor whose resistance changes with the intensity of light, which makes them perfect to use as a light sensor. Read more about them <a href='http://en.wikipedia.org/wiki/Photoresistor' class='bbc_url' title='External link' rel='nofollow external'>on Wikipedia</a>.<br /><br /><span style='font-size: 18px;'>5mm Red LED (ultra bright)</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/2/5mm-red-led-ultra-bright-677.jpg' alt='Posted Image'  /></span><br />An LED is a Light Emitting Diode. When a voltage is applied across the element with the correct polarity, it lights up! LEDs have become extremely popular in recent years in everything from computers, to cell phones, to brake lights, to flash lights, etc. In fact, it's a fair bet that almost every electronic device these days has at least one LED in it!<br /><br /><span style='font-size: 18px;'>5mm Green LED (ultra bright)</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/31/5mm-green-led-ultra-bright-674.jpg' alt='Posted Image'  /></span><br />Ditto.<br /><br /><span style='font-size: 18px;'>Resistors</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/143/1-4w-100-ohm-carbon-film-resistor-460.jpg' alt='Posted Image'  /></span><br /><br />The kit comes with 10 of each of the following resistors:<br />1/4W 330Ω Carbon Film Resistor<br />1/4W 1kΩ Carbon Film Resistor<br />1/4W 4.7kΩ Carbon Film Resistor<br />1/4W 10kΩ Carbon Film Resistor<br /><br />The 330Ω resistors are great for LEDs. The 10kΩ resistors are commonly used for pull-up or pull-down resistors. The 1kΩ and 4.7kΩ are handy to have around.<br /><br /><span style='font-size: 18px;'>Heavy Duty Double Sided Tape</span><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/products/images/233/heavy-duty-double-sided-tape-818.jpg' alt='Posted Image'  /></span><br />We will use the heavy duty double sided tape to attach the servos and battery holder to the chassis.<br /><br /><br /><span style='font-size: 24px;'>Making the chassis</span><br /><br />OK, now that you know what's in the kit, it's time to start making your first robot! To keep costs down, we're going to get creative when it comes to the chassis. You can buy a nice plastic or metal chassis if you'd like, but in this tutorial we're going to use cardboard (you can even use the box your kit came in!).<br /><br />Take a cardboard box (if you ordered the <a href='http://www.foxytronics.com/products/234-arduino-starter-robotics-kit' class='bbc_url' title=''>Arduino Starter Robotics Kit</a> from here, the Priority Mail box it comes in works great) and draw a rectangle that's about 4-1/2" x 7":<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_43728.jpg' alt='Posted Image'  /></span><br /><br />Now, cut out the rectangle, and when you're done, it should look like this:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_18572.jpg' alt='Posted Image'  /></span><br /><br /><br /><span style='font-size: 24px;'>Mounting the servo motors</span><br /><br />Next, we're going mount the servo motors to the chassis. The easiest way to do this is to use the heavy duty double sided tape provided in the kit. Cut two 1-1/2" pieces off the strip of double sided tape, peel one side and firmly press it onto the servos:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_22015.jpg' alt='Posted Image'  /></span><br /><br />Note: Notice in the above picture how both of the servos are oriented so that the wires come out downwards. It doesn't matter which side you stick the tape on, but you want the tape to be on the same side of each servo when the shafts of the servos are aligned, as in the above picture, so that the wheels will be aligned.<br /><br />Now attach the servos to the chassis by peeling off the other side of the tape and then pressing them firmly down onto the cardboard. We'll mount the servos so that the shafts are in the middle of the chassis (so if the chassis is 7" long, position the servos so that the shafts are at 3-1/2") so that the robot can spin on a dime, but you can mount them all the way on the end if you want to.<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_58704.jpg' alt='Posted Image'  /></span><br /><br />Note how the shaft is at 3-1/2" and how the edge of the servo is parallel to the edge of the cardboard. Here's what it looks like after mounting the other servo:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_62292.jpg' alt='Posted Image'  /></span><br /><br /><br /><span style='font-size: 24px;'>Working on the top side</span><br /><br />Now that we've got the motors mounted, it's time to work on the top side of the robot. We're going to mount the battery back in a similar manner to the motors. Cut off two 2" pieces of double sided tape and put them on the battery pack like this:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_47775.jpg' alt='Posted Image'  /></span><br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_15487.jpg' alt='Posted Image'  /></span><br /><br />Then put the battery pack on one end of the chassis, making sure that the plug doesn't stick over the edge of the chassis (if it does it might interfere with the wheel):<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_16807.jpg' alt='Posted Image'  /></span><br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_33934.jpg' alt='Posted Image'  /></span><br /><br />Mount the breadboard to the opposite end of the chassis using the adhesive already on it:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_41467.jpg' alt='Posted Image'  /></span><br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_55838.jpg' alt='Posted Image'  /></span><br /><br />The Arduino will go in-between the breadboard and battery holder. You can use two 2" pieces of double sided tape to mount it to the chassis, or just let it sit there. Either way works just fine.<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_47910.jpg' alt='Posted Image'  /></span><br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_21039.jpg' alt='Posted Image'  /></span><br /><br />Each servo comes with a bag of accessories, including large round discs that we'll be using as our wheels:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_3449.jpg' alt='Posted Image'  /></span><br /><br />Go ahead and attach the wheels to the servos by firmly pressing them onto the shafts. You'll immediately notice that the chassis will fall on one end, like this:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_44626.jpg' alt='Posted Image'  /></span><br /><br /><br />Once the batteries are in the battery holder, it will actually fall the other way. We need a way to keep the robot level. An elegant solution would be to use a small caster wheel, but we'll continue being frugal and use some more cardboard to solve the problem.<br /><br />Cut out a piece of cardboard about 1" x 4". Fold the cardboard in half so that you have this:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_14055.jpg' alt='Posted Image'  /></span><br /><br />Fold each end down so that you have what almost looks like a paper plane, with the chassis resting level on the wings:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_52465.jpg' alt='Posted Image'  /></span><br /><br />I put the batteries in the battery pack so that the chassis would fall the right way, and notice that I used a piece of scotch tape to hold the piece of cardboard in a "V" shape.<br /><br /><strong class='bbc'>Note:</strong> In many battery holders, the batteries fit very tightly and the springs aren't strong enough to push them forward, so make sure to push the batteries forward so that ends are making contact with the other side of the holder.<br /><br />Cut out another piece of double sided tape about 2" long and stick the piece of cardboard onto it:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_29886.jpg' alt='Posted Image'  /></span><br /><br />Now attach it under the end of the chassis:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_49639.jpg' alt='Posted Image'  /></span><br /><br />That completes the chassis!<br /><br /><span style='font-size: 24px;'>Electrical</span><br /><br />Now that the chassis is done, it's time to move on to the electronics!<br /><br />Start by mounting the ultrasonic sensor onto the breadboard (a cable is provided in case you don't want to put the sensor directly in the breadboard), then use red wire to connect VCC to the power rail on the breadboard, black wire to connect GND to the GND rail on the breadboard, and white wire to connect Trig and Echo to pins 8 and 9 on the Arduino, respectively:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_55027.jpg' alt='Posted Image'  /></span><br /><br />The last thing we need to do is connect the motors. To do this, cut two 3-pin segments off the header strip provided in the kit, then push the black piece to about the middle:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_8697.jpg' alt='Posted Image'  /></span><br /><br />Put the header pins somewhere in the breadboard and connect the servo wires to them, making sure to orient both the wires the same way:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_75156.jpg' alt='Posted Image'  /></span><br /><br />Next, we need to use wires to get power from the Arduino to the ultrasonic sensor. Use a red and black wire to connect the 5V and GND pins on the Arduino to the positive (red) and GND (blue) rails on the breadboard. After doing that, connect the white wires on the servo cables (the PWM signal wires) to pins 12 and 13 on the Arduino:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_21545.jpg' alt='Posted Image'  /></span><br /><br />You can also see how I tidied up the servo wires with a zip tie. It's always good to keep wiring as clean as possible. I tidied up the wiring on the bottom as well:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_70342.jpg' alt='Posted Image'  /></span><br /><br />Now, use jumper wires to connect the red and black wires on the servo cables together:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_20442.jpg' alt='Posted Image'  /></span><br /><br />The last step is to get power to the motors. We could power them from the Arduino, but this is a bad practice because the motors might draw too much current and overload the voltage regulator on the Arduino. The best practice is to have a separate battery pack for the motors, but the next best thing is to have wires going directly from the battery pack to the motors. Read the <a href='http://www.foxytronics.com/tutorials/_/arduino/how-to-use-continuous-rotation-servos-with-arduino-r32' class='bbc_url' title=''>how to use continuous rotation servos with Arduino</a> tutorial to see how to power the servos directly from the batteries.<br /><br /><strong class='bbc'>Note:</strong> Heed the warnings in the above tutorial about polarity (i.e. about getting the ground point on the battery holder correct)!<br /><br />The easiest way to connect the wires to the battery pack is to take the battery out, put the wise between the spring coils, then put the battery back in (the battery will hold the wire in). Your robot should now look like this:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_67906.jpg' alt='Posted Image'  /></span><br /><br />Give yourself a pat on the back -- you just finished making your first robot!<br /><br /><br /><span style='font-size: 24px;'>Programming</span><br /><br />All right, now that we've finished building our robot, it's time to make it actually do something! We do this by writing a program (or "sketch", in Arduino terms) and downloading it to the microcontroller. Head on over to the <a href='http://www.foxytronics.com/tutorials/_/arduino/getting-started-with-arduino-windows-r30' class='bbc_url' title=''>getting started with Arduino tutorial</a> to learn how to get the Arduino IDE and driver installed.<br /><br /><span style='font-size: 18px;'>1. Hello World</span><br />Once you have the Arduino IDE and driver installed on your computer, we can go ahead and start writing our first sketch, the classic "Hello World" program! Fire up the Arduino IDE and you'll be presented with a blank sketch. Go ahead and copy and paste the code below into the IDE, then click the right arrow button in the upper left hand corner to build and upload the program to your Arduino:<br /><br /><br /><br /><br /><br /><pre class='prettyprint lang-auto linenums:0'>void setup()
{
    Serial.begin(9600);
}


void loop()
{
  Serial.println("Hello World!");
}

</pre>The status text in the lower left hand corner of the Arduino IDE will say "Done uploading" when the program has finished uploading to the Arduino. Once the program has been uploaded, click on the magnifying glass in the upper right hand corner to open up the Serial Monitor. A window will open up, and you should see the text "Hello World!" being printed over and over again in it. Congratulations, you just wrote your first Arduino sketch!<br /><br />Let's go over the program above. The first function is <a href='http://arduino.cc/en/Reference/Setup' class='bbc_url' title='External link' rel='nofollow external'>setup()</a>:<br /><pre class='prettyprint lang-auto linenums:0'>void setup()
{
    Serial.begin(9600);
}
</pre>Setup() is a special function that the Arduino runs when it first turns on. This is where we put our initialization code, which in this case was configuring and starting the serial port on the Arduino. The line Serial.begin(9600) tells the Arduino to configure the onboard serial port to operate at 9600 baud and to start it.<br /><br />The next function is <a href='http://arduino.cc/en/Reference/Loop' class='bbc_url' title='External link' rel='nofollow external'>loop()</a>:<br /><pre class='prettyprint lang-auto linenums:0'>void loop()
{
  Serial.println("Hello World!");
}
</pre>Loop() is a special function that the Arduino keeps calling as long as it is turned on. This function is where the body of Arduino programs reside. The body of this program is really simple and only has one line, which is the command Serial.println(). This command tells the Arduino to send the string of text passed as a parameter over the serial port (in this case, back to our computer).<br /><br /><span style='font-size: 18px;'>2. Servos</span><br /><br />Now lets write another program. This time, let's use the servo motors on our robot to make it drive forward!<br /><pre class='prettyprint lang-auto linenums:0'>#include &lt;Servo.h&gt;

// create servo objects
Servo leftMotor;
Servo rightMotor;

void setup()
{
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
  leftMotor.write(180);
  rightMotor.write(180);
}
</pre>The first line of the program includes an <a href='http://arduino.cc/en/Reference/Libraries' class='bbc_url' title='External link' rel='nofollow external'>Arduino library</a>. There are many libraries that have been written for Arduino and they allow us to easily do things that would otherwise require us to write a lot of code. In this program, we're including the Servo library, which will allow us to easily use our servo motors.<br /><br />The next two lines create Servo objects, which we name leftMotor and rightMotor (it's always a good idea to name variables descriptively).<br /><br />This time, instead of starting the serial port in the setup() function, we use the Servo .attach() method to tell the servo objects what pins the servos are connected to on the Arduino. The .attach() function has an Arduino pin number as a parameter, so since we connected our servos to pins 12 & 13 that's what we pass to the functions as arguments.<br /><br />Now that the servo objects know what pins our servos are connected to on the Arduino, we can use the Servo .write() function to control the servo's direction and speed. The Servo .write() method takes a number between 0 and 180 as a parameter. The reason for this range is that a "standard servo" (one that is not continuous rotation like ours) usually rotates about 180 degrees, so having the parameter be 0-180 makes sense (i.e. with a standard servo passing 0 would make it turn all the way one direction, passing 90 would make it go to the middle, and passing 180 would make it go all the way the other direction). With a continuous rotation servo, passing the value 0 to the .write() function tells the servo to turn at full speed one direction, 90 makes it stop (also called "neutral"), and 180 makes it turn full speed the other direction. Go ahead and upload the program and see what happens!<br /><br /><strong class='bbc'>Note:</strong> You might want to put something under the robot to keep the wheels from touching the ground, because they're going to start turning once the program uploads to the Arduino!<br /><br />With any luck, when the program finished uploading, the wheels started turning! But wait, look at the directions of the wheels -- they're turning different directions! Why is this happening?<br /><br />The reason is that one of the servos is "flipped" with respect to the other since it's pointing in the opposite direction, so in order to make the wheels on the servos both turn in the same direction, we need to send opposite values to them! Try changing the parameters in one of the .write() functions to 0, and after uploading you'll find that both wheels turn in the same direction!<br /><br /><span style='font-size: 18px;'>3. Ultrasonic sensor</span><br /><br />The next thing we're going to do is learn to use the Ultrasonic sensor on the robot to avoid obstacles. Read the tutorial on <a href='http://www.foxytronics.com/tutorials/_/arduino/using-an-hc-sr04-ultrasonic-sensor-with-arduino-r33' class='bbc_url' title=''>using an HC-SR04 ultrasonic sensor with Arduino</a> to learn how the sensor works, then take a look at the program below:<br /><pre class='prettyprint lang-auto linenums:0'>const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int utlrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;


void setup()
{
    Serial.begin(9600);
  
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(utlrasonic2EchoPin, INPUT);
}


void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay &gt;= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        timeLoopDelay = millis();
    }
}


void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(utlrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void debugOutput()
{
    if((millis() - timeSerialDelay) &gt; serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm: ");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}
</pre>This program is from the <a href='http://www.foxytronics.com/tutorials/_/arduino/using-an-hc-sr04-ultrasonic-sensor-with-arduino-r33' class='bbc_url' title=''>using an HC-SR04 ultrasonic sensor with Arduino</a> tutorial. It's more complicated than our previous ones, but it's explained in that tutorial (so read it if you haven't already). Go ahead and upload the program to your Arduino.<br /><br />After uploading the program to your Arduino, open up the Serial Monitor again (click the magnifying glass in the upper right-hand corner), and you should see something similar to the following:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_18351.png' alt='Posted Image'  /></span><br /><br />The distance being printed out will depend on how far your robot is away from an object. Try putting your hand in front of the ultrasonic sensor on your robot and moving it back and fourth and the distance being printed out will change accordingly. Pretty cool, huh?<br /><br /><span style='font-size: 18px;'>4. Navigating with the ultrasonic sensor</span><br /><br />Now that we know how to use the servos to make the robot drive around, and how to use the ultrasonic sensor to measure obstacles in front of us, let's combine this knowledge to write a program that has the robot drive around and avoid obstacles!<br /><br />Using the ultrasonic code above as a starting point (since it's more complicated than the servo program), let's add the servo code to it.<br /><br />The first step is include the servo library by adding the following to the top of the file:<br /><pre class='prettyprint lang-auto linenums:0'>#include &lt;Servo.h&gt;
</pre>Next, add the lines to create the servo objects:<br /><pre class='prettyprint lang-auto linenums:0'>// create servo objects
Servo leftMotor;
Servo rightMotor;
</pre>Then configure the servo objects in the setup() function:<br /><pre class='prettyprint lang-auto linenums:0'>leftMotor.attach(13);
    rightMotor.attach(12);
</pre>At this point, you should have the following:<br /><pre class='prettyprint lang-auto linenums:0'>#include &lt;Servo.h&gt;

// create servo objects
Servo leftMotor;
Servo rightMotor;

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int utlrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;

void setup()
{
    Serial.begin(9600);
  
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(utlrasonic2EchoPin, INPUT);
    
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay &gt;= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        timeLoopDelay = millis();
    }
}


void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(utlrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void debugOutput()
{
    if((millis() - timeSerialDelay) &gt; serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm: ");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}
</pre>Now we need to add the code to make the robot drive around. Before we jump in to writing the code, let's think about what we want to happen:<ul class='bbcol decimal'><li>If there's nothing in front of the robot, drive forward</li><li>If there is an obstacle in front of the robot, turn left</li></ul>We will use a <a href='https://en.wikipedia.org/wiki/Finite-state_machine' class='bbc_url' title='External link' rel='nofollow external'>finite state machine</a> to accomplish this.<br /><br />Start by adding a new function to the file, named stateMachine(), then add a call to this function inside of loop() so that it gets called every loop. Now, add the following towards the top of the file where the other variables are (above the setup() function):<br /><pre class='prettyprint lang-auto linenums:0'>// define the states
#define DRIVE_FORWARD     0
#define TURN_LEFT         1

int state = DRIVE_FORWARD; // 0 = drive forward (DEFAULT), 1 = turn left
</pre>We are going to use the state variable to keep track of whether the robot should be driving forward or turning left (you can add more states, like turning right, later if you want to). Now we can begin working on the stateMachine() function itself.<br /><br />To implement the state machine, we're going to use if-else statements, like this:<br /><pre class='prettyprint lang-auto linenums:0'>if(state == DRIVE_FORWARD) // no obstacles detected
    {
        
    }
    else if(state == TURN_LEFT) // obstacle detected -- turning left
    {
        
    }
</pre>We set the state variable to DRIVE_FORWARD by default, so when the robot turns on, the program will branch into the first if statement. Inside the DRIVE_FORWARD state, we need to check whether there is an obstacle in front of us and if there isn't, we want to drive forward. If there is an obstacle in front of us, we want to change the state to TURN_LEFT. So let's go ahead and add the code to do that:<br /><pre class='prettyprint lang-auto linenums:0'>if(ultrasonic2Distance &gt; 6 || ultrasonic2Distance &lt; 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
        {
            // drive forward
            rightMotor.write(180);
            leftMotor.write(0);
        }
        else // there's an object in front of us
        {
            state = TURN_LEFT;
        }
</pre>Next, we need to write the code for the TURN_LEFT state. In this state, we want to start the robot turning left and keep it turning left until it has turned 90 degrees. Here's the code for the TURN_LEFT state:<br /><pre class='prettyprint lang-auto linenums:0'>unsigned long timeToTurnLeft = 1100; // it takes around 1.1 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) &lt; timeToTurnLeft) // stay in this loop until timeToTurnLeft (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        
        state = DRIVE_FORWARD;
</pre>This may be a little confusing, so let's break it down line by line:<ul class='bbcol decimal'><li>timeToTurnLeft: we put the amount of time it takes our robot to turn 90 degrees in here (in milliseconds)</li><li>turnStartTime: we store the amount of time that the Arduino has been powered on in here</li><li>millis()-turnStartTime: this gives us the amount of time that the robot has been turning left</li><li>while((millis()-turnStartTime) &lt; timeToTurnLeft): the program will stay in the while loop until the robot has been turning for timeToTurnLeft milliseconds</li></ul>Inside the while loop, we make the robot turn. The while loop keeps looping until the proper time has elapsed, then the program exists the while loop and the state is set back to DRIVE_FORWARD.<br /><br /><strong class='bbc'>Note:</strong> Using a loop in this manner is quick and easy, but it's not a good practice because it stops other things from happening in the program. A loop like this is called "blocking" because the program gets "stuck" in it for a certain amount of time. We will show a non-blocking example later.<br /><br />OK, so at this point you should have something similar to the following:<br /><pre class='prettyprint lang-auto linenums:0'>#include &lt;Servo.h&gt;

// create servo objects
Servo leftMotor;
Servo rightMotor;

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int utlrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;

// define the states
#define DRIVE_FORWARD     0
#define TURN_LEFT         1

int state = DRIVE_FORWARD; // 0 = drive forward (DEFAULT), 1 = turn left

void setup()
{
    Serial.begin(9600);
  
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(utlrasonic2EchoPin, INPUT);
    
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay &gt;= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        stateMachine();
        
        timeLoopDelay = millis();
    }
}


void stateMachine()
{
    if(state == DRIVE_FORWARD) // no obstacles detected
    {
        if(ultrasonic2Distance &gt; 6 || ultrasonic2Distance &lt; 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
        {
            // drive forward
            rightMotor.write(180);
            leftMotor.write(0);
        }
        else // there's an object in front of us
        {
            state = TURN_LEFT;
        }
    }
    else if(state == TURN_LEFT) // obstacle detected -- turn left
    {
        unsigned long timeToTurnLeft = 1100; // it takes around 1.1 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) &lt; timeToTurnLeft) // stay in this loop until timeToTurnLeft (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        
        state = DRIVE_FORWARD;
    }
}


void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(utlrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void debugOutput()
{
    if((millis() - timeSerialDelay) &gt; serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}
</pre>Go ahead and upload the program to the Arduino and see what happens!<br /><br /><iframe id="ytplayer" class="EmbeddedVideo" type="text/html" width="640" height="390" src="https://youtube.com/embed/LJ5Yg6_Dq1E?html5=1&fs=1" frameborder="0" allowfullscreen webkitallowfullscreen /></iframe><br /><br />Success! It drives forward until an obstacle is detected, then turns left! The robot is driving so slowly because the plastic wheels are slipping on my tile floor. Rubber wheels would be a nice upgrade for this 'bot!<br /><br />And there you have it, a fully functional robot!<br /><br />Remember, this tutorial just covered one easy way to make an inexpensive robot. By being creative, you can make all sorts of different kinds of robots inexpensively. Check out the other projects on here to get ideas and inspiration. Here are some other website's you might find helpful as well:<br /><br /><a href='http://letsmakerobots.com/view/node/list/robot' class='bbc_url' title='External link' rel='nofollow external'>http://letsmakerobot...node/list/robot</a><br /><a href='http://forums.trossenrobotics.com/robots.php' class='bbc_url' title='External link' rel='nofollow external'>http://forums.trosse....com/robots.php</a><br /><a href='http://www.societyofrobots.com/robots.shtml' class='bbc_url' title='External link' rel='nofollow external'>http://www.societyof...om/robots.shtml</a><br /><a href='http://communityofrobots.com/list/robot' class='bbc_url' title='External link' rel='nofollow external'>http://communityofro....com/list/robot</a><br /><br />Go forth and create!]]></description>
		<pubDate>Fri, 24 May 2013 21:44:40 +0000</pubDate>
		<guid isPermaLink="false">7f1de29e6da19d22b51c68001e7e0e54</guid>
	</item>
	<item>
		<title>Using an HC-SR04 ultrasonic sensor with Arduino</title>
		<link>http://www.foxytronics.com/tutorials/_/arduino/using-an-hc-sr04-ultrasonic-sensor-with-arduino-r33</link>
		<description><![CDATA[In this tutorial we will be learning to use an HC-SR04 ultrasonic distance sensor with an Arduino Uno.<br /><br />The following hardware was used in this tutorial:<br /><br />1 x <a href='http://www.foxytronics.com/products/185-arduino-compatible-uno-rev3' class='bbc_url' title=''>Arduino</a><br />1 x <a href='http://www.foxytronics.com/products/132-hc-sr04-ultrasonic-distance-sensor' class='bbc_url' title=''>HC-SR04 Ultrasonic Distance Sensor</a><br /><br />An ultrasonic distance sensor works in a similar manner to radar and sonar. The transmitter on the sensor sends out a sound wave that will eventually bounce off an object and return to the receiver on the sensor if there is an object in the sensor's range. If the sensor receives the sound wave, indicating an object is in front, the sensor then makes the echo pin high for the duration of the sound wave's travel time. So, as an example, if a "ping" was sent out and the round trip time (going out and hitting the object, then returning to the receiver) took 25 us, then the echo pin would be pulled high for 25us. We can use the speed of sound to convert this time to a distance, letting us know how far away objects are.<br /><br />The speed of sound in air is <span rel='lightbox'><img class='bbc_img' style="vertical-align: middle;" src="http://latex.codecogs.com/gif.latex?&#092;mathrm{c_{air} = 331.3\frac{m}{s}+0.606^{\circ}C^{-1}\cdot[temp \ in \ ^{\circ}C]}" border="0"/></span><br /><br />Assuming the average room temperature is around 20C (68F), then the speed of sound in air is:<br /><br /><span rel='lightbox'><img class='bbc_img' style="vertical-align: middle;" src="http://latex.codecogs.com/gif.latex?&#092;mathrm{c_{air} = 331.3\frac{m}{s}+0.606^{\circ}C^{-1}\cdot20 ^{\circ}C = 343.42\frac{m}{s}}" border="0"/></span><br /><br /><span rel='lightbox'><img class='bbc_img' style="vertical-align: middle;" src="http://latex.codecogs.com/gif.latex?&#092;mathrm{c_{air} = 0.034342\frac{cm}{\mu s}}" border="0"/></span><br /><br /><span rel='lightbox'><img class='bbc_img' style="vertical-align: middle;" src="http://latex.codecogs.com/gif.latex?&#092;mathrm{c_{air} = 29.11886\frac{\mu s}{cm}}" border="0"/></span><br /><br />In our Arduino sketch, we first need to configure the digital I/O pins used for the ultrasonic sensor. I define the pins at the top of my program so that it's easy to see what pins are being used and to change them:<pre class='prettyprint lang-auto linenums:0'>
// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int utlrasonic2EchoPin = 9;
</pre>I have more than one ultrasonic sensor in my project, so I named them ultrasonicXTrig/EchoPin.<br /><br />Then, in your setup() function, actually configure the pins:<pre class='prettyprint lang-auto linenums:0'>
// ultrasonic sensor pin configurations
pinMode(ultrasonic2TrigPin, OUTPUT);
pinMode(utlrasonic2EchoPin, INPUT);
</pre>The next step is to send out the sound wave:<pre class='prettyprint lang-auto linenums:0'>
// ultrasonic 2
digitalWrite(ultrasonic2TrigPin, HIGH);
delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
digitalWrite(ultrasonic2TrigPin, LOW);
</pre>The Trig pin should be low to begin with, then we send out a pulse at least 10us in duration by setting the Trig pin high for that amount of time, then we set the Trig low again.<br /><br />Now we are ready to read the travel time from the sensor (the following code should be immediately after the above code):<pre class='prettyprint lang-auto linenums:0'>
// make these variables global if you need to access their values in other functions
int ultrasonic2Duration;
int ultrasonic2Distance;

ultrasonic2Duration = pulseIn(utlrasonic2EchoPin, HIGH);
ultrasonic2Distance = (ultrasonic2Duration/2)/29;
</pre>The pulseIn() function returns the period of a pulse on the selected pin. In our case, this gives us the travel time of the sound wave. The next order of business is to convert this time to what we really want, which is distance. We first divide the travel time by two because we want the distance to the object, not the distance to the object and back. Then, divide by the speed of sound we calculated before, and we have the distance in cm!<br /><br />Here is a full example program:<pre class='prettyprint lang-auto linenums:0'>
/**********************************
* Author: Nathan House            *
* Website: www.foxytronics.com    *
* Modified: 3/21/13               *
**********************************/

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int utlrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;


void setup()
{
    Serial.begin(9600);
  
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(utlrasonic2EchoPin, INPUT);
}


void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay &gt;= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        timeLoopDelay = millis();
    }
}


void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(utlrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void debugOutput()
{
    if((millis() - timeSerialDelay) &gt; serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm: ");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}
</pre>After uploading the program to your Arduino, open the serial monitor and you should see the following:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_23/gallery_1_23_18351.png' alt='Posted Image'  /></span><br /><br />The distance will be different, but you should see the text "ultrasonic2Distance: " followed by the the measured distance.]]></description>
		<pubDate>Thu, 21 Mar 2013 17:35:36 +0000</pubDate>
		<guid isPermaLink="false">9fc3d7152ba9336a670e36d0ed79bc43</guid>
	</item>
	<item>
		<title>How to use continuous rotation servos with Arduino</title>
		<link>http://www.foxytronics.com/tutorials/_/arduino/how-to-use-continuous-rotation-servos-with-arduino-r32</link>
		<description><![CDATA[This tutorial covers using continuous rotation servos with an Arduino.<br /><br />The following hardware was used in this tutorial:<br /><br />1 x <a href='http://www.foxytronics.com/products/185-arduino-compatible-uno-rev3' class='bbc_url' title=''>Arduino</a><br />1 x <a href='http://www.foxytronics.com/products/54-solderless-breadboard-400-contacts' class='bbc_url' title=''>Breadboard</a><br />1 x <a href='http://www.foxytronics.com/products/80-6-x-aa-battery-holder-with-dc-power-jack-9v' class='bbc_url' title=''>6 x AA Battery Holder</a><br />2 x <a href='http://www.foxytronics.com/products/68-springrc-sm-s4303r-continuous-rotation-servo' class='bbc_url' title=''>SpringRC SM-S4303R Continuous Rotation Servos</a><br />And a few pieces of <a href='http://www.foxytronics.com/category/18-wire/' class='bbc_url' title=''>hookup wire</a>, or <a href='http://www.foxytronics.com/products/90-breadboard-jumper-wire-bundle-70-pack' class='bbc_url' title=''>breadboard jumper wires</a><br /><br />First, we need to talk about how to power the servos. While the servos can be powered from the 5V pin on the Arduino, doing so is not a good idea because the servos could potentially draw more current than the Arduino 5V regulator can provide, which would result in damage to the Arduino. A much better alternative is to power the servos directly from a battery pack (either the same battery pack powering the Arduino or, even better, a separate battery pack).<br /><br />The SpringRC SM-S4303R Continuous Rotation Servos that we're using require a voltage between 4.8V and 6V, so using four AA batteries - which each, depending on the type, provide somewhere between 1.2V and 1.5V - will supply the motors with somewhere between 4.8V and 6V, which is precisely what we need. The easiest way I've found to do this is to stick a red wire and a black wire into the second spring from the left and the last spring on the right on the battery pack, respectively, which will provide the output voltage we need:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_41195.jpg' alt='Posted Image'  /></span><br /><br /><strong class='bbc'>Note: </strong>It is VERY IMPORTANT to ensure that the black (GND) wire is connected to the GND point on the battery holder. Connecting the black wire to the first spring on the left in the above picture would not be connecting it to the GND point, which could cause serious problems. Trace the black wire that goes into the battery holder to the spring it connects to and that is the GND point. If you see sparks when you plug in the black or red wires, something is wrong. If you have a multimeter, this would be a great time to use it to verify you're plugging in the wires to the right places.<br /><br />Run the red and black wires to a breadboard and then plug the servo power wires into the breadboard. The third wire on the servos (the white one) is the signal wire that tells the servos which direction and how fast to turn. The signal wires on the servos need to be hooked up to digital pins on the Arduino.<br /><br />Here's a rough diagram of the circuit:<br /><br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_50326.jpg' alt='Posted Image'  /></span><br /><br /><br />Fritzing doesn't have the battery holder or servo that we are using, so the diagram looks a little different than in real life, but you get the idea.. The second servo is hooked up the same as the first, but with the signal wire connected to another digital pin (I used pins 12 & 13 for my servos).<br /><br />Using servos with Arduino is extremely easy due to the Servo library included with the IDE. Here's a simple sketch that configures the servo objects and sets the servos to neural (i.e. tells them not to turn).<pre class='prettyprint lang-auto linenums:0'>
#include &lt;Servo.h&gt; // include the Servo library

// create the servo objects
Servo leftMotor;
Servo rightMotor;

void setup()
{
  leftMotor.attach(13);  // attaches the servo on pin 13 to a servo object
  rightMotor.attach(12); // attaches the servo on pin 12 to a servo object
}

void loop()
{
  leftMotor.write(90);  // 90 is neutral, so neither of the servos should turn
  rightMotor.write(90);
}
</pre>After creating the servo objects and telling them what pin the servos are connected to, we just use the Servo.write(value) function to set the speed and direction. The Servo.write(value) function has a parameter who's value may be between 0 and 180 (this is because standard servos typically rotate between 0 and 180 degrees), where 0 is full speed one direction, 90 is neutral, and 180 is full speed the other direction.<br /><br />That's how simple it is!]]></description>
		<pubDate>Sat, 02 Feb 2013 15:57:54 +0000</pubDate>
		<guid isPermaLink="false">65ded5353c5ee48d0b7d48c591b8f430</guid>
	</item>
	<item>
		<title>Flashing an LED with Arduino</title>
		<link>http://www.foxytronics.com/tutorials/_/arduino/flashing-an-led-with-arduino-r31</link>
		<description><![CDATA[Blinking an LED is sort of the "hello world" of the microcontroller world. The phrase comes from the fact that printing text to the console in C is essentially the easiest thing that can be done, so printing "hello world" is something of a tradition for beginning programmers.<br /><br />With microcontrollers, we can't simply print text out to the console (although in a later tutorial we will see how to send data back to the computer, or to a character LCD), so we blink an LED. That's how we roll.<br /><br />This tutorial requires the following hardware:<br /><br />1 x <a href='http://www.foxytronics.com/products/185-arduino-compatible-uno-rev3' class='bbc_url' title=''>Arduino</a><br />1 x <a href='http://www.foxytronics.com/products/54-solderless-breadboard-400-contacts' class='bbc_url' title=''>Breadboard</a><br />1 x <a href='http://www.foxytronics.com/products/145-1-4w-330-ohm-carbon-film-resistor' class='bbc_url' title=''>1/4W 330Ω Resistor</a> (anything 250Ω or greater will work)<br />1 x <a href='http://www.foxytronics.com/products/2-5mm-red-led-ultra-bright' class='bbc_url' title=''>5mm Red LED</a><br />And a few pieces of <a href='http://www.foxytronics.com/category/18-wire/' class='bbc_url' title=''>hookup wire</a>, or <a href='http://www.foxytronics.com/products/90-breadboard-jumper-wire-bundle-70-pack' class='bbc_url' title=''>breadboard jumper wires</a>.<br /><br />We are going to use a digital output on the Arduino to turn an LED on and off. However, the LED cannot be connected directly between the digital output on the Arduino and GND because it would draw too much current and burn out, so we need to put a resistor in series with the LED.<br /><br />Here is a diagram of our circuit:<br /><br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_41416.jpg' alt='Posted Image'  /></span><br /><br />The Arduino Uno has 14 digital pins that can be used as digital inputs, digital outputs, or PWM pins, and the first two can be used for serial communication. As shown in the diagram, we will connect one of these digital pins to the anode (positive) leg of the LED (the longer one), then use a resistor to connect the cathode (negative) leg of the LED (the shorter one) to GND.<br /><br />Once you have built your circuit, it's time to program the Arduino and tell it what to do!<br /><br />Here is the program that makes it work:<pre class='prettyprint lang-auto linenums:0'>
/**********************************
* Program: LED Blink              *
* Author: Nathan House            *
* Website: www.foxytronics.com    *
* Date Modified: 1/1/13           *
**********************************/

#define STATUS_LED 7 // use pin 7 to turn the LED on and off (any digital pin will work)

/*
* setup() is called when the Arduino is turned on or resets
*/
void setup()
{                
  pinMode(STATUS_LED, OUTPUT); // define the pin we're using as an output
}

/*
* loop() is continuously run while the Arduino has power
*/
void loop() 
{
  blinkLED(); // call the blinkLED() function every loop
}

/*
* this function blinks an LED
*/
void blinkLED()
{
  digitalWrite(STATUS_LED, 1); // turn the LED on
  delay(500);                  // wait 0.5 sec
  digitalWrite(STATUS_LED, 0); // turn the LED off
  delay(500);                  // wait 0.5 sec 
}

</pre><br />The program - or in Arduino terminology, "sketch" - is pretty simple:<br /><br />The setup() function is called automatically when the Arduino powers up and inside it we configure the digital pin we are using as a digital output (as opposed to an digital input).<br /><br />The loop() function is continuously called automatically, and inside it we call our blinkLED() function which is where the code that makes the LED blink resides (we could have put the LED code inside of loop(), but it is always a good idea to "modularize" your code).<br /><br />The blinkLED() function has two different types of statements in it. The first is a call to digitalWrite(), which allows us to change the logic level on the digital output specified as the first argument. The first time we call digitalWrite(), we pass it a "1" (or HIGH) to turn on the LED. The second time we call digitalWrite(), we pass it a "0" (or LOW) to turn off the LED. After each call to digitalWrite() we call delay(), which delays the number of passed milliseconds (in this case, 500 ms, or 0.5 sec).<br /><br />That's it! At this point your LED should be merrily blinking! Pat yourself on the back - this may seem like a small project, but it's a stepping stone to more advanced projects in the coming tutorials.]]></description>
		<pubDate>Sat, 02 Feb 2013 03:12:26 +0000</pubDate>
		<guid isPermaLink="false">1afa34a7f984eeabdbb0a7d494132ee5</guid>
	</item>
	<item>
		<title>Getting started with Arduino (Windows)</title>
		<link>http://www.foxytronics.com/tutorials/_/arduino/getting-started-with-arduino-windows-r30</link>
		<description><![CDATA[Arduinos have become extremely popular over the past few years due to their ease of use, large user base, and relatively low cost. In this tutorial we'll explain how to get your Arduino Uno up and running.<br /><br /><br /><span style='font-size: 18px;'>Installing the IDE</span><br /><br />The first thing you'll need to do is <a href='http://arduino.cc/en/main/software' class='bbc_url' title='External link' rel='nofollow external'>download the Arduino IDE</a> for your platform. Once downloaded, unzip the folder and move it to your desired location. The Arduino IDE does not come with an installer and instead runs out of the folder you just unzipped (hence why you should move it to wherever you want the program to reside -- I would suggest either your My Documents folder or Program Files as a good place to put the folder). For convenience, create a shortcut to the executable (arduino.exe) on your desktop or task bar.<br /><br /><br /><span style='font-size: 18px;'>Installing the driver</span><br /><br />The next step is to install the driver for the Arduino. In this tutorial we are using an Arduino Uno, but the process should be similar for other Arduino boards.<br /><br />Connect power to your Arduino and connect it to your computer using a USB cable. Depending on what operating system you are using, what happens next will vary, but most likely you will see a popup bubble in the system tray (the lower right hand corner of the screen where the clock is) that says something like "Found New Hardware," or "Installing device driver software."<br /><br />If you are using Windows XP and the Found New Hardware Wizard comes up, <a href='http://www.ladyada.net/learn/arduino/lesson0-win.html' class='bbc_url' title='External link' rel='nofollow external'>follow the instructions here</a>.<br /><br />If you are using Windows 7, the Found New Hardware Wizard probably won't come up, so install the driver using the method below:<br /><br />Open Device Manager using one of the methods below:<ul class='bbcol decimal'><li>Click Start, then type in Device Manager and click on the Device Manager search result.</li><li>Press the Windows key + R, which will bring up the Run dialog, type in devmgmt.msc and press OK.</li></ul>Once Device Manager is open, expand the "Other devices" tree and there should be an entry titled "Unknown Device":<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_32872.png' alt='Posted Image'  /></span><ul class='bbcol decimal'><li>Right click on "Unknown device."</li><li>If "Update Driver Software" is an option, click it and go to step 4, otherwise go to step 3.</li><li>If "Update Driver Software" is <strong class='bbc'>not</strong> an option, click on "Properties, " select the Driver tab and click "Update Driver."</li><li>At this point, a popup window similar to the one below should have appeared. Select the "Browse my computer for driver software" option.</li></ul><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_22325.png' alt='Posted Image'  /></span><br /><br />You should now see the window below:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_39009.png' alt='Posted Image'  /></span><br /><br />Click "Browse" and browse to the Arduino IDE folder you unzipped earlier. Once you find the folder, expand it and select the Drivers folder:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_14523.png' alt='Posted Image'  /></span><br /><br />Click OK to close out the browse dialog. Make sure "Include subfolders" is checked (like it is in the previous image), then click Next.<br /><br />A warning dialog will likely popup:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_21339.png' alt='Posted Image'  /></span><br /><br />If it does, take a leap of faith and click "Install this driver software anyway."<br /><br />After it finishes installing, you should see the following window:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_4710.png' alt='Posted Image'  /></span><br /><br />That's it, the Arduino driver is now installed!<br /><br /><br /><span style='font-size: 18px;'>Creating a project</span><br /><br /><span style='font-size: 14px;'>Now we will create our first project using the Arduino IDE.</span><br /><br />Run the <span style='font-size: 14px;'>Arduino IDE</span> and a new "sketch" should open by default:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_1377.png' alt='Posted Image'  /></span><br /><br />Although the Arduino is connected to the computer using a USB cable, it actually uses a virtual COM port (i.e. an emulated serial port) for communication. Because of this, we need to tell the IDE what COM port the Arduino is connected to. To do this, click on Tools-&gt;Serial Port and select the COM port that the Arduino is connected to:<br /><br /><span rel='lightbox'><img class='bbc_img' src='http://www.foxytronics.com/uploads/gallery/album_21/gallery_1_21_26143.png' alt='Posted Image'  /></span><br /><br />If there is more than one port listed and you're not sure which one the Arduino is connected to, open Device Manager and expand the "Ports (COM & LPT)" tree to which COM port is associated with the Arduino.<br /><br />You're now ready to being writing your first Arduino program!]]></description>
		<pubDate>Sat, 26 Jan 2013 18:32:01 +0000</pubDate>
		<guid isPermaLink="false">9b8619251a19057cff70779273e95aa6</guid>
	</item>
</channel>
</rss>