Building an Arduino Robot, Part II: Programming the Arduino
Welcome to the second article in the tutorial series in which I’m building a remote controlled Arduino based vehicle robot.
Here is the list of articles I have published:
- Part I: Hardware Components
- Part II: Programming the Arduino (this article)
- Part III: Assembling the Robot
- Part IV: A (Not So) Basic Robot Firmware
- Part V: Avoiding Obstacles
- Part VI: Remote Control
In the previous article I introduced you to my project. I showed you the parts I bought and what features I wanted to implement in my little remote controlled robot vehicle.
Today I will introduce you to the programming side of my project. This is a pretty long article that is heavy in information that can be applied to most Arduino projects. By the end of it you will know the basics of programming sensors, motors and other devices connected to the Arduino, and will also have an understanding of how a wireless remote controller can communicate with the Arduino board over Bluetooth. I encourage you to get the parts I listed in the previous article and try these tests along with me.
The Arduino Pins
The Arduino board communicates with connected devices via its input and output pins, so I’ll quickly go over what these pins are.
Here is a diagram of a typical Arduino board.
On the left side you have the USB port (grey box) and the power input jack (black box). During testing I will be powering my Arduino with a USB cable, but the final robot will receive power from a battery box connected to the power input jack.
On the top, from right to left there are 14 pins labeled 0 to 13. These are the digital pins. These pins can be individually configured as inputs or outputs, meaning that digital data can be read or written from connected devices on these pins. Since these pins are digital, they have only two possible states, HIGH and LOW.
Some of the digital pins have preassigned functions. Pins 0 and 1 are also labeld RX and TX respectively. These are used by the serial communication hardware to send and receive data. Pin 13 has a LED attached to it on most Arduino boards, so it is a convenient pin to send simple visual information out to the real world. The pin 13 LED is located below the pin 13 itself, labeled with the letter L in the diagram above. Pins 3, 5, 6, 9, 10 and 11 are marked with a ~ or a PWM label, short for pulse width modulation. These pins are capable of producing simulated analog output over a digital line. These can be used, for example, to light a LED at different levels of intensity.
The next pin on the top from right to left is GND, short for ground. The ground is what closes a circuit and allows the electric current to flow uninterrupted. Connected devices can connect their own GND pin here.
The next pin on the top left is labeled AREF, short for analog reference. This pin is rarely used, it tells the Arduino how to configure the voltage range of the analog pins.
Some Arduino boards have more pins to the left of AREF. Since these are not in all boards I will not discuss them here.
On the bottom right we have six pins labeled 0 to 5. These are the analog input pins. Unlike the digital pins which have only two possible states, an analog pin can have 1024 possible states, according to the voltage applied to it. Typically the voltage range goes up to 5V, but the range can be changed by applying the desired maximum voltage to the AREF pin. Also unlike the digital pins which can be configured as inputs or outputs, the analog pins can only be inputs.
An interesting property of the analog pins is that they can be used as digital pins as well, with pin numbers 14 to 19. You will see later that an important part of any project is allocating pins to components. If you are running short of digital pins keep in mind that you have six more available to you.
Continuing on the bottom from right to left we find a pin labeled VIN. This pin provides direct access to the voltage provided by the power supply. So for example, if you power your Arduino with a 9V power supply through the power jack, then this pin gives you 9V.
The next two pins are labeled GND and are two more ground pins, exactly like the one in the top row. They are here just for convenience.
The next two pins are labeled 5V and 3.3V and just return those voltages, regardless of what the voltage of the power supply is.
Next we have the RESET pin. When this pin is connected to GND the board will reset. So this is a handy pin to build an external reset button.
As with the top row of pins, some boards have a more pins on the bottom left that I will not cover here. If you are interested in finding out what extra pins in your board do you should consult the documentation for your board.
Programming the Arduino with Sketches
By now you are probably wondering how can you make the Arduino do things.
The easiest way to program the board is with the Arduino software, which is free and open source, and available for Windows, Mac OS X and Linux users.
For more detail: Building an Arduino Robot, Part II: Programming the Arduino
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
Cheapest PCB Prototype: Only $2 for 10pcs 10×10cm PCBs, 24 hours Quick Turn, DHL Delivery in 3 Days