Mikroe Buggy: Line follower with MikroC for PIC code


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.

This blog post is more than two years old.

Compared to the previous projects involving the MikroElektronika Buggy, this one is a bit more difficult, an 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.

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. So, here is the spacer, and 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

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 sensor 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. The sensor outputs are connected as follows:

[table id=6 /]

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

Mikroe Buggy: line follower code in MikroC for PIC

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

A few final words

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 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 a analog input – applying 3.3V directly to the photo transistor will likely damage it. The workaround is to use another analog input pin, for example from the mikroBUS sockets on the Clicker2 board.

And finally, if you try to replicate his project I would love to hear from you experience. Don’t hesitate to comment, and I will gladly help you overcome any issues.


Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.