Driving the Buggy – Arduino style

Some of the links on this blog are affiliated links. READ MORE

In my latest post I wrote about programming the Clicker2 for PIC32MX using MPIDE, which is an Arduino-like IDE. Now I make one step forward and use this Cliker2 board as the “brain” of the MikroEketronika Buggy.

As with many other MikroElektronika solutions the hardware of this project is very easy to implement. Just stick the USB UART into socket #1 of the Clicker2 for PIC32MX board, then install the Clicker 2 in the Buggy. There is only one single issue to overcome, but that’s no big deal: as the USB UART works only in socket#1 of the Clikcker2 board, the USB cable is obstructed by the raiser card for the mikroBUS #3 of the Buggy.

Connecting the USB cable to the Buggy - cable touches the rear mikroBUS card

The USB cable touches the rear mikroBUS card

One might wish to install longer pins on the Clicker 2 for PIC32MX and on the USB UART to gain some extra height. If you can find one, a 90 deg USB cable can also avoid this issue.

Programming the Buggy in MPIDE

Now we come to the fun side: programming the Buggy in Arduino language. As the hardware is imposed, the first step is to identify which Arduino pins correspond to the Buggy features. Here’s what I found so far:

[table id=3 /]

As you can see in the above table, the headlights are controlled by Arduino pin 22, main beam is controlled by pin 42 and breaks are turned on by pin 28. Left turn signal is pin 62, while right turn signal is pin 24. The following code example flashes the LEDs on the buggy:

Driving the motors

Unlike the ubiquitous L298N which controls the power in many robots and rovers, the MikroElektronika Buggy uses a pair of DRV8833 H-bridges from Texas Instruments. An interesting choice, which allows for some extra control as compared against the L298N. I won’t say that you should read the datasheet, although is highly recommended, but at least look at the H-bridge logic in the table below:

[table id=4 /]

Some terms in this table attract attention: fast decay and slow decay. What the heck is this? Read the following paper for a full explanation what slow decay and fast decay are, and how the current flows in these modes. Believe me, it’s worth reading it.

Another thing to consider is how the motors are connected inside the Buggy. There are two DRV8833 ICs, each controlling the motors on one side. IN1 and IN3 are connected in parallel, OUT1 drives the front motor and OUT2 drives the rear motor. So, to drive the Buggy the following signal combination can be used:

[table id=5 /]

If you look on the original PIC32 example code from Libstock, you’ll find out that only fast decay is used. I will keep the same approach for the moment. If you wish, it takes only a few keystrokes to change to slow decay.

Based on the pin correspondence in the first table, the pins to control the left motors are 25 (PWMA) and 29 (PWMB), while the right motors are controlled by pins 37 (PWMC) and 53 (PWMD).
With all the above considerations, here’s a demo code to control the motors in the Buggy:

You may notice that when I run the Buggy at slower speed I first drive the Buggy at full PWM for a few milliseconds to overcome the inertia. Think of a regular car – you start in the first gear, which gives you the maximum torque. Here we have no gears to change, only a fixed redactor. So, to get the maximum torque you need to apply full power to the motor.

Second, by changing the PWM ratio between left and right motors the Buggy can make a wide turn. Sharp turns can be achieved either by stopping one side, or by turning it into reverse (which is the way I did in this code example). In a way, the Buggy turns more like a tank rather than a car.

And some final thoughts

Controlling the Buggy in MPIDE allows to benefit from the best of two worlds – the excellent hardware of the MikroElektronika Buggy and the huge amount of already written Arduino code. There are also libraries for almost any sensor you can imagine, but sometimes they don’t work on PIC32 – this happens because some coders use “pure” Atmel 8-bit C code inside the libraries to achieve some code speedup. There is nothing wrong with that, but it may prove an nuisance as the line of microcontrollers that can be programmed in Arduino is growing. In many cases all you have to do is search for a library from a different coder.

Furthermore, MPIDE is free. So, if you are a hobbyist or a student looking for an idea for the final year project, this might be a good starting point – and a good way to save some money if you don’t already own some PIC32 development tools.

The code examples that I gave here are just that – some demos. Please feel free top enhance them and add you own contribution. I’ll be glad if you share your results.

And don’t forget: Have fun!


Leave A Reply