Using pyboard to control two DC motors
Tags: python, embedded, electronics
Published: 2020-11-09 19:24:05

This is part three in my series of building a small robot car. You can find the previous articles below:

Part One: [First Steps Creating a Robot with MicroPython and Pyboard](https://www.halldorstefans.com/first-steps-creating-a-robot-with-micropython-and-pyboard/)

Part Two: [My First Arduino Built Robot Car](https://www.halldorstefans.com/first-arduino-build-robot-car/)

I ended the last part wondering whether I should add some sensors to my robot, or replace the Arduino controller for a pyboard controller.

Because my initial idea for this project was to use the pyboard, I decided I would start by replacing the controller and then later I could add some sensors.

## Updating pyboard firmware

I've owned the pyboard for a few years now, but I've never really done much with it. I, therefore, suspected that the firmware might be out of date. So, the first item on the list was to update the firmware.

The [documentation](https://github.com/micropython/micropython/wiki/Pyboard-Firmware-Update) for this is excellent, but I did run into a minor problem because I kept downloading the [firmware for pyboard v1.0, but I had v1.1](https://micropython.org/download/pybv1/c). Once I figured that out, it was quick and easy.

## Running one motor

Once the pyboard was up to date, it was time to find out how to control one of the DC motors with it.

After a bit of research, I found [this article](https://rk.edu.pl/en/updating-pyboard-firmware-latest-micropython-features-and-fixes/#3). Using that and the [offical documentation for PWM](http://docs.micropython.org/en/latest/pyboard/quickref.html#pwm-pulse-width-modulation), my initial code in [micropython](https://micropython.org/) looked like this:

``` python
from pyb import Pin, Timer

p1 = Pin('X1')
tim2 = Timer(2, freq=1000)
ch1 = tim2.channel(1, Timer.PWM, pin=p1)
ch1.pulse_width_percent(50)
```

And this is how I connected it together, with the Maker Drive:

![Maker-Drive and pyboard layout](https://hsblogimages.blob.core.windows.net/ghost/makerDrivePyboardOneMotor.png)

[And this worked perfectly!](https://twitter.com/HalldorStefans/status/1323372242705764354)

## Adding the second motor

Based on the fact that I was using the X1 and X2 pins to run the first motor, and [the layout](https://micropython.org/resources/pybv11-pinout.jpg), I reckoned I could use the X3 and X4 pins for the second motor.

So after adding the second motor, I made some changes to the code:

``` python
from pyb import Pin, Timer

p1 = Pin('X1')
tim2 = Timer(2, freq=1000)
ch1 = tim2.channel(1, Timer.PWM, pin=p1)
ch1.pulse_width_percent(50)

p3 = Pin('X3')
ch3 = tim2.channel(1, Timer.PWM, pin=p3)
ch3.pulse_width_percent(50)
```

And voila! Both motors were running. [What a beauty!](https://twitter.com/HalldorStefans/status/1323376774202478592)

## Final touches

Once everything was working correctly, I put it together with the chassis and updated the code a bit to make it drive into a kind of a circle :D

You can see the current code and setup on my [GitHub](https://github.com/halldorstefans/pyboardRobotCar).

I'm really pleased how well this went and honestly thought it would take longer.

I'm going to put this project on to the side for a bit now. But the next item on the agenda for this project is to add some sensors to it and make them influence the driving.

---

Thank you for reading. For weekly updates from me, you can sign up to my [newsletter.](https://email.halldorstefans.com/)