In this Instructable we will show you how to upgrade a cheap analog line follower into an intelligent Arduino line follower by replacing the LM393 comparator IC by an intelligent Attiny85 and Arduino sketch.
Line follower robots can be bought cheaply; you can buy DIY kits through Banggood, Ebay and Aliexpress from about 7 Euros. Ours is built from a PCB and two geared motors. For following lines, it contains two bright LEDs and two photo-sensitive cells to detect the line, and some analog logic and two transistors to control the motors. Two variable resistors allow you to calibrate the sensors.
You may have built one yourself, and noticed that calibration is cumbersome and needs to be redone when for instance ambient light conditions change. In our case, we even needed to enlarge the resistance of one of the variable resistors in order for the line follower to follow any line. So my son and myself became dissatisfied quickly with the project and set it aside.
As the analog 'logic' of our line follower is comprised of a LM393 Dual Differential Comparator in an 8 pin DIP socket, we wondered whether we could slot in an Attiny85 which is also in an 8 pin DIP socket, and program the Attiny85 with the Arduino programming environment such that it would automatically calibrate the sensors at start-up. Future developments we have in mind are more intelligence and a bluetooth connection.
The Atmel Attiny85 is a full system on a chip, with processor, oscillator, memory (EEPROM, RAM, flash) and mutiple programmable analog and digital inputs and outputs. It can be programmed with the Arduino IDE. In summary it is a complete computer in an extremely small package, but costs only approx. 1 Euro (or dollar) including shipping from China.
Unfortunately, the Attiny85 IC is not pin to pin compatible with the LM393, so we needed a little piece of protoboard to reroute the signals. The protoboard contains the Attiny85 on top of it, while we soldered some standard header pins to the bottom, such that it could slot in the existing DIP socket of the line follower. In this way, no change to the line follower is needed, and we can always go back to the analog version. We loaded a simple Arduino sketch onto the Attiny85, which functions much better than the original LM393 analog logic. The line follower now calibrates the sensors every time at start-up, and we get much better performance of the car. My son suggested we would share this project on Instructables, to inspire others. Let's go.
Step 1: This Is What You Will Need
Figures: (top) a piece of protoboard, the Attiny85, an 8 pin DIP socket and some wire, (bottom left) a bunch of headerpins, (bottom right) and USBTiny programmer (with homemade daughter board).
This is what you need:
2. Attiny85, for instance from AliExpress, 1 Euro.
3. An 8 pin socket for the Attiny85, for instance from AliExpress, less than 1 Euro.
5. Some headerpins, for instance from: AliExpress, less than 1 Euro. Alternatively, you can use some wire.
Besides this, you need the following:
- Soldering iron, (leadfree) solder, some thin wire to make the connections. Saw or scissors to cut the protoboard.
- A way to program the Attiny85 chip. We use the USBTiny progammer (third picture above, with homemade daughter board) which we would recommend, but instructions can be found elsewhere to program the Attiny85 with another Arduino, Arduino or parallel port of your PC.
- PC with Arduino IDE installed. You would need to install the Attiny board in the boards manager, instructions can be found here.
Step 2: Soldering the Protoboard
Figures: (top) Fritzing schematic. Note the Attiny (socket) sits on top of the protoboard, the header pins at the bottom. (bottom) Soldered protoboard, ready to be placed on line follower.
Preparing the protoboard
There are at least two types of prototype boards. The one we used contains strips of copper, others contain 'islands'. In fact the 'islands' type are easier to use, especially the ones that have copper on both sides of the board. In case you use the 'strip' type, you would need to cut the strips at the right places with a drill or knife, to make sure there is no short between the adjacent pins.
First cut a strip from the protoboard to the correct size. This can be done with scissors, or a saw or whatever you are comfortable with. The size should be 4 rows high, by approx. 15 rows wide.
Soldering the protoboard
Break off two rows of four pins from the header strip. Then solder the two rows to the bottom of protoboard, at the right side of the protoboard, see the second picture. Note that there should be two rows of holes in between the headers, such that the headers can fit in the 8 pin socket on the line follower (again, in case you are using a strip type board, the strips should be cut inbetween the headers to prevent a short circuit). In my case, I selected the copper to be at the top of the protoboard, but you may also choose the other way around.
Then solder the 8 pin DIP socket for the Attiny85 to the top of the protoboard. Depending on whether you have through-hole metalisation of the protoboard, this may be tricky but in our case it worked ok. You may wonder why we are not soldering the Attiny85 straight onto the protoboard, but this would make reprogramming of the Attiny85 cumbersome, so a socket is highly advisable.
Making the connections
Subsequently make all of the connections as indicate on the first picture shown above. The colour of the wiring is of course irrelevant. We used thin black wire, but anything goes as long as you make sure there are no bridges/shorts.
Step 3: Programming the Attiny85
Figures: (top) line follower with LM393 logic IC, note the notch on the LM393 pointing in the driving direction and (bottom) the line follower with the Attiny85 slotted in, please note the Attiny pin 1 (small hole on Attiny casing) is also pointing in the driving direction.
The Arduino code is shown here (see below) and is pretty self explanatory. Some guidelines within the sketch are given to port it to other Arduinos like Pro mini, Nano or Uno.
First make sure you have the Arduino IDE set up and the Attiny85 board is available via the boards manager (see above). Select Attiny85, and set speed to 1MHz (internal). Higher processor speeds will work, but are not needed.
Open the sample code in Arduino IDE, burn bootloader, and compile and upload the code onto the Attiny85.
If everything went OK, place the Attiny85 in the socked on the protoboard you prepared, with the dot (pin 1) pointing to the driving direction. Place the protoboard in the socket on the Line Follower, as indicated above. Then power up the line follower and hope there is no magic smoke.
After start-up, the code takes a 2 second pause. During this, you have time to place the Line Follower on a white (or light) surface. Subsequently, the motors vibrate to indicate calibration is going to take place, and vibrate again to signal calibration is complete. One second later the Line Follower will start to drive.
The calibration is done by taking ten samples of the light sensitive cells, adding them to the calibration variable, and subsequently dividing the calibration variable by 10 to calculate the average. Subsequently, an offset is added to the calibration value, to prevent that small changes in brightness cause false readings of black and white. As is indicated in the sample code, you can play with the value of the constant SensorOffset to improve performance of black and white detection.
The driving logic itself is pretty simple. In case both sensors see white or both sensors see black, both motors spin. In case one of the sensors sees black, and the other white, the correct motor is stopped to make a turn.
Please note that as the motors are driven with PNP transistors, a HIGH output signal stops the motors, and a LOW signal lets the motors spin.
The line follower contains two variable resistors, which are meant to calibrate the sensors while the line follower was running with the original LM393 IC. In principle, the position of the resistors is irrelevant after the Attiny85 conversion, but is advised to place the resistors in the middle position before powering up the line follower.
This concludes this Instructable. We hope it will inspire you to implement something similar, improve the intelligence, and/or add remote control capabilities.