Buggy line follower with MikroC PIC code

Teodor Costachioiu
Sep 25, 2015 · 5 min read

Take one Buggy, one Clicker2 for PIC18FJ and one PROTO click. Add one QTR-8A line sensor from Pololu. Build one small (wood) spacer. That’s all for the hardware. Write some nice code lines in MikroC for PIC. Yes, that’s a simple recipe to turn the Mikroe Buggy into a line follower robot.

Compared to the previous projects involving the MikroElektronika Buggy, this one is a bit more difficult, and it requires some modifications of the original Buggy hardware. As usual, I tried to keep the number of modifications to a minimum, and I tried to make the things non-permanent, so all the hardware (including the Buggy) can be further reused in other projects.

The project is based on the Buggy with a Clicker2 for PIC18FJ. The reason for this choice is that I desired to complicate things a little bit — there are only a few code examples of line followers made with PIC microcontrollers, and I can benefit from having a MikroC PRO for PIC license. Besides this, a QTR-8A line follower sensor from Pololu is used, the reason for this choice is that the QTR-8A is the only line sensor that can be easily configured for 3.3V operation to match the Buggy hardware (all other members of the QTR family are designed to work on 5V supply). The QTR-8A has eight line sensors, of which only three are used in this project. The sensor bar even allows for easy separation of six of the line sensors, as you can see in the pictures below. The remaining two sensors were put aside for another project.

QTR-8A line sensor

The sensor bar has to be kept at a small distance from the line to work properly. As such, I made a small spacer out of a wood piece. Of course, any type of plastic works fine, but if you try to replicate this project you will find out that wood is easier to work with considering the tools available to the average maker. The picture below shows the final result. The slit in the wood pieces is for an easier cable management, and it can be replaced by a series of 5mm holes, or you can even skip it.

Wood spacer for QTR-8A

The sensor bar is attached to the spacer with some strong double-sided tape. The whole assembly attaches to the buggy with 3M Dual Lock velcro — this is to allow for easy removal and to leave the Buggy unchanged. In the middle picture, we can see the whole sensor assembly, of which only the highlighted sensors are used in this project. The three remaining sensors are not connected.

Finally, the connection wires are attached. To complete the assembly I used one PROTO Click inserted in the mikroBUS socket #2 of the Buggy, with some screw-type connectors. The QTR-8A was configured for 3.3V operation by placing the bridge over the 3.3V bypass pins and is powered from the 3.3V pin of the PROTO click.

The trick here is to observe that on the mikroBUS socket#2 there are three pins with analog input function: the AN, RST, and CS pins.

With these steps completed, the hardware is ready and we can proceed to the next phase: writing the code.

MikroC code for the line follower

The code is written in MikroC Pro for PIC, the section of the code dedicated to the reading of the line sensor being heavily inspired by the Arduino QTR code library from Pololu:

The Mikroe Buggy performs just fine in this application. Actually I’m confident that I can go with this hardware setup to any line follower competition — and win. The only changes that I would do to make the Buggy competition-worthy would be at the spacer for the sensors, particularly I would replace the velcro with strong double tape.

Another thing to consider is expanding the number of sensors used. In this project, I use only three sensors, but two more sensors can be easily connected to other ADC channels (think the A/D inputs of the mikroBUS sockets on the Clicker2 for PIC18FJ).

The code is not optimized, and in fact, it has some minor flaws. It’s up to you to improve it.

Can it work with Arduino?

Yes, with some changes. If you decide to take the other way and use the Clicker2 for PIC32 as the robot brain and MPIDE to run Arduino code, you must consider that the mikroBUS#2 CS is used by the flashing LED of the bootloader. That means that you cannot use it as an analog input — applying 3.3V directly to the phototransistor will likely damage it. The workaround is to use another analog input pin, for example from the mikroBUS sockets on the Clicker2 board.

Originally published at https://electronza.com on September 25, 2015. Moved to Medium on May 3, 2020.


DIY electronics projects and more

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store