How to control brushless Motor with raspberry pi 
Tags: beginners, programming, tutorial, computerscience
Published: 2021-12-01 20:04:40

**What is raspberry pi?
**
In simple words raspberry pi is small computing Engine (Computer) and it is also providing GPIO pins to get input or output form any other device or sensor.
You can also check [this Wikipedia page](https://en.wikipedia.org/wiki/Raspberry_Pi) for more information.

Controlling [1000kv brushless motors](https://www.flyrobo.in/a2212_1000kv_brushless_motor_for_rc_airplane) using Your Raspberry Pi is easy! If you're looking to control one [1000 kv motor](https://www.flyrobo.in/a2212_1000kv_brushless_motor_for_rc_airplane)  or construct big project on the Raspberry Pi The basic principles are the same however the equipment required will differ. You can choose all Raspberry Pi for this project (Zero, Zero W 3, 4, etc.)

This guide will walk you through the basics of making use of the TB6612 to drive only one DC motor that I will use for the Amazon Echo Furby project. This tiny chip is provided by Adafruit (and other companies) however I would favor the Adafruit version because it comes with diodes to safeguard both motors and the Pi. Some chips require soldering an individual diode circuit (such such as Sparkfun TB6612FNG) I'd prefer to stay clear of this.



**

## Component Required
**
[1000 kv brushless motor
](https://www.flyrobo.in/a2212_1000kv_brushless_motor_for_rc_airplane)
raspberry pi zero
[30a ESC](https://www.flyrobo.in/simonk_30a_2-3s_brushless_esc_for_rc)

In essence, your Pi's GPIO (general general purpose input/output) pins are connected directly to your driver board, providing the board with power as well as data signals that instruct the motor to begin running by through the PWM (pulse-width modulation). This is followed by writing a basic Python script to toggle the GPIO pins to HIGH or LOW, thereby activating the motor!

A different power source provides the driver board with power, that is then directly transferred to motors. Therefore there are two sources of power all together:


- Motors require power (provided via an external battery or power supply)
- The motor driver's power source (provided via the Pi's GPIO pins).

This is a relatively simple and easy project that's an excellent exploration of PWM and GPIO on the Raspberry Pi GPIO and PWM. Let's get started!



**

## Selecting a driver board as well as motor(s)

**
In my case I will only have to power one motor. If, however, you were to create an Raspberry Pi robot or rover or something similar then you may need to run multiple motors. In this scenario you'll need the driver board that is able to support many motors (and an additional power source to power them). In this tutorial we'll work with the Adafruit TBR6612 (same as the TB6612FNG).

This board is able to operate two DC motors or one stepper motor -with a an max current limit of 1.2A. If you are choosing a motor, make sure you don't go over this amperage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hoby83hv0ldua4wvxs4b.png)

I suggest using a breadboard for prototyping and test connections before soldering.

The diagram of the wiring below illustrates the connections you'll must make to create the circuit for your driver board TB6612. I've also made the Fritzing diagram for those who are interested in this kind of thing.


Keep in mind (per the illustration) the power that is supplied to the motor is not the power supplied to the the driver's logic circuit! 

`STBY = Pin 13 (GPIO #21)

Motor A:
PWMA = Pin 7 (GPIO #4)
AIN2 = Pin 11 (GPIO #17)
AIN1 = Pin 12 (GPIO #18)

Motor B:
BIN1 = Pin 15 (GPIO #22)
BIN2 = Pin 16 (GPIO #23)
PWMB = Pin 18 (GPIO #24)`

**

## Writing the code

**
The code in the following article is a great way to drive the DC motors. This sample code drives them clockwise for five second and then counterclockwise for 5 second.

Connect to the Raspberry Pi to create an entirely new file:


How to Connect to a Raspberry Pi Remotely via SSHHow to Connect to a Raspberry Pi Remotely via SSH

The most popular (and most commonly) method to connect to your Pi to execute commands.

`sudo nano ~/motor/motor.py`

Copy the code above Save, then close:

**

## Drive a single motor:
**

`
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT) # Connected to PWMA
GPIO.setup(11, GPIO.OUT) # Connected to AIN2
GPIO.setup(12, GPIO.OUT) # Connected to AIN1
GPIO.setup(13, GPIO.OUT) # Connected to STBY

GPIO.output(12, GPIO.HIGH) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2

GPIO.output(7, GPIO.HIGH) # Set PWMA

GPIO.output(13, GPIO.HIGH)

time.sleep(5)

GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.HIGH) # Set AIN2

GPIO.output(7, GPIO.HIGH) # Set PWMA

GPIO.output(13, GPIO.HIGH)

time.sleep(5)

GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2
GPIO.output(7, GPIO.LOW) # Set PWMA
GPIO.output(13, GPIO.LOW) # Set STBY`


**

## Drive two motors:
**

`
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT) # Connected to PWMA
GPIO.setup(11, GPIO.OUT) # Connected to AIN2
GPIO.setup(12, GPIO.OUT) # Connected to AIN1
GPIO.setup(13, GPIO.OUT) # Connected to STBY
GPIO.setup(15, GPIO.OUT) # Connected to BIN1
GPIO.setup(16, GPIO.OUT) # Connected to BIN2
GPIO.setup(18, GPIO.OUT) # Connected to PWMB

GPIO.output(12, GPIO.HIGH) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2
GPIO.output(15, GPIO.HIGH) # Set BIN1
GPIO.output(16, GPIO.LOW) # Set BIN2


GPIO.output(7, GPIO.HIGH) # Set PWMA
GPIO.output(18, GPIO.HIGH) # Set PWMB

GPIO.output(13, GPIO.HIGH)

time.sleep(5)

GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.HIGH) # Set AIN2
GPIO.output(15, GPIO.LOW) # Set BIN1
GPIO.output(16, GPIO.HIGH) # Set BIN2

GPIO.output(7, GPIO.HIGH) # Set PWMA
GPIO.output(18, GPIO.HIGH) # Set PWMB

GPIO.output(13, GPIO.HIGH)

time.sleep(5)

GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2
GPIO.output(7, GPIO.LOW) # Set PWMA
GPIO.output(13, GPIO.LOW) # Set STBY
GPIO.output(15, GPIO.LOW) # Set BIN1
GPIO.output(16, GPIO.LOW) # Set BIN2
GPIO.output(18, GPIO.LOW) # Set PWMB`

**

## Execute the code
**

`sudo python ~/motor/motor.py
`