Introduction: Ben - a Light Following Breadboard Arduino Robot
Ben the Light Following Breadboard Arduino Robot is the second robot I have made to help teach robotics to high school students in a class I teach voluntarily. The first robot also has its own Instructable which can be found here: https://www.instructables.com/id/James-Your-first-Arduino-Robot/
The video shows the construction process however it is in fast motion and I will cover some of the more complicated things here more in depth.
So to make ben you will need the following components:
· A small sheet of acrylic
· A breadboard
· An Arduino Nano
· Two Continuous Rotation Servos
· Two hobby wheels (I used model aeroplane wheels)
· A castor wheel
· 9V battery
· 4.8V rechargeable battery pack (or just 4 AA’s in a battery case)
· Two Light Dependant Resistors
· Two 10,000 ohm Resistors
· Some breadboard jumper cables
· Double Sided Foam Tape
· Adhesive Velcro dots
· A Power Switch (not necessary but would be handy)
You won’t need any tools for the construction however you could replace the Double Sided Foam Tape with Hot Glue in which case you would need a Hot Glue Gun.
Now the first thing that may need further explanation is the use of the Light Dependant Resistors. Light Dependant Resistors (or LDR’s) are resistors whose value changes depending on the amount of ambient light, but how can we detect resistance with Arduino? Well you can’t really, however you can detect voltage levels using the analog pins, which can measure (in basic use) between 0-5V. Now you may be asking “Well how do we convert resistance values into voltage changes?”, it’s simple, we make a voltage divider. A voltage divider takes in a voltage and then outputs a fraction of that voltage proportional to the input voltage and the ratio of the two values of resistors used. The equation for which is:
Output Voltage = Input Voltage * ( R2 / (R1 + R2) )
Where R1 is the value of the first resistor and R2 is the value of the second.
The circuit schematic for which looks like this
A diagram of this in our situation looks a little something like this
Now this still begs the question “But what resistance values does the LDR have?”, good question. The less amount of ambient light the higher the resistance, more ambient light means a lower resistance. Now for the particular LDR’s I used their resistance range was from 200 – 10 kilo ohms, but this changes for different ones so make sure to look up where you bought them from and try to find a datasheet or something of the sort.
Now in this case R1 is actually our LDR, so let’s bring back that equation and do some math-e-magic (mathematical electrical magic).
Now first we need to convert those kilo ohm values into ohms:
200 kilo-ohms = 200,000 ohms
10 kilo-ohms = 10,000 ohms
So to find what the output voltage is when we are in pitch black we plug in the following numbers:
5 * ( 10000 / (200000 + 10000) )
The input is 5V as that is what we are getting from the Arduino.
The above gives 0.24V (rounded off).
Now we find what the output voltage is in peak brightness by using the following numbers:
5 * ( 10000 / (10000 + 10000) )
And this gives us 2.5V exactly.
So these are the voltage values that we are going to get into the Arduino’s analog pins, but these are not the values that will be seen in the program, “But why?” you may ask. The Arduino uses an Analog to Digital Chip which converts the analog voltage into usable digital data. Unlike the digital pins on the Arduino that can only read a HIGH or LOW state being 0 and 5V the analog pins can read from 0-5V and convert this into a number range of 0-1023.
Now with some more math-e-magic we can actually calculate what values the Arduino will actually read. Because this will be a linear function we can use the following formula:
Y = mX + C
Where; Y = Digital Value
Where; m = slope, (rise / run), (digital value / analog value)
Where; C = Y intercept
The Y intercept is 0 so that gives us:
Y = mX
m = 1023 / 5 = 204.6
Digital value = 204.6 * Analog value
So in pitch black the digital value will be:
204.6 * 0.24
Which gives approximately 49.
And in peak brightness it will be:
204.6 * 2.5
Which gives approximately 511.
Now with two of these set up on two analog pins we can create two integer variables to store their values two and do comparison operators to see which one has the lowest value, turning the robot in that direction.
Now that was probably the most complex thing about the whole robot build however there is just one more thing that I would like to mention and it’s to do with using servos with Arduino.
There are several tutorials and diagrams on the internet showing that you must connect the voltage in of the servo up to the 5V rail of the Arduino and the ground of the servo to the ground of the Arduino, this is dangerous! Servos can draw a lot of current, and in most cases that current draw will be more than the voltage regulator on the Arduino can supply, this will lead to bad things happening. The proper way to hook servos to your Arduino is to use an external power supply. In Bens cause I am running the continuous rotation servos of a 4.8V rechargeable Ni-Cd battery pack, this is ideal as the servos operate well from 4.8-6V, 6V being the peak charge voltage of the battery pack.
Now you may be tempted to just hook up V+ of the battery to the V+ of the servos and the GND of the battery pack to the GND of servos and the signal pins to the Arduino, this won’t work either! You need to remember that electricity needs to flow from one ‘point’ back to its original point, not connecting the ground of the servos and battery pack to the Arduino’s ground won’t allow the electricity to flow from the signal pins.
Here is a diagram showing the proper circuitry
Combining the two diagrams shown earlier gives the complete circuitry required to make Ben.
Now I won’t explain the code as it is heavily commented and should pretty much explain itself.
We have a be nice policy.
Please be positive and constructive.