August 05, 2015 | Electronics |
In this project, we will control two motors over BLE using the Nordic nRF51-DK. To do this, we will use a motor driver based on the Toshiba TB6612FNG chip. We will use GPIOs and PWM to communicate with this board.
Before you read further, you might want to look at some of my previous articles on nRF51822 programming, since we’re going to use the same development setup here.
- nRF51822 Begins - nRF-DK, GCC, ADC, UART/BLE
- External nRF51822 SWD Programming using the nRF51-DK
- nRF51-DK PWM & GPIOTE test with S110 SoftDevice
- Talking to Ultrasonic Distance Sensor HC-SR04 using nRF51822
- Controlling an RGB LED with Nordic nRF51-DK (nRF51822/nRF51422)
Communicating with TB6612FNG
The pins PWMA and PWMB control the speeds of the two DC motors. (The speed being proportional to the duty cycle of the PWM signal.) The pins (AIN1, AIN2) and (BIN1, BIN2) control the direction of rotation of the motors. The STBY “standby” pin has to be high for any of the controls to work. The VCC pin is the supply for the board logic, and has to be in the range of 2.7 to 5.5 V. The VMOT pin is the motor power supply, which has to be in the range of 4.5 to 13.5 V. (AO1, AO2) and (BO1, BO2) are output supplies to the two motors.
The speed and direction of the motors can be controlled in various ways, as shown in an excerpt from the Toshiba TB6612FNG data sheet below:
Here is how the nRF51-DK is hooked up to the TB6612FNG in this project:
In my case, the motors are rated at around 4.5 V. I supply VMOT via a 11.2 V LiPO battery regulated down to 5V using a 7805 regulator IC.
The PWM module is initialized in the code as follows:
In the code above, we set up PWM1 instance with a frequency of 1000 Hz on the PWMA and PWMB pins.
To control the motors, we use the Nordic nRFToolboxApp. The app has a configurable keypad which send strings to the nRF51-DK over the Nordic UART Service when the buttons are pressed. We check for these strings and take appropriate action in the code as follows:
Here is how you start and stop the motors, and set the speed of rotation:
As you can see above, setting STBY to LOW will stop the motors. Setting the duty cycle will effect the speed of the motors proportionately. Here’s how you set the direction of rotation of the motors:
Flipping the input pin states switches the direction of motors above.
In my case, the motors are fitted to a 2WD robot chassis with a caster wheel. So to make the wheels turn, I can just reduce the speed of one of the wheels, as follows:
In the above code, I reduce the speed, make the turn by just rotating one of the wheels, wait for a bit, and then restore speed. This has the action of changing the direction of movement towards left or right.
Calling turn() above directly from the NUS data handler messes up the PWM signal. (The Nordic implentation of nRF51 PWM has several known bugs.) So I side-stepped this problem by just setting a flag and executing the turn in the main loop:
You can refer to the source code link below to see how all these pieces fit together.
See the BLE motor control in action below:
You can get the complete source code for this project here:
- nRF51 Series Reference Manual Version 3.0.
- Toshiba TB6612FNG data sheet.
Need help with a hardware project or product? Drop us an email at firstname.lastname@example.org. We offer consulting services on AVR and Nordic nRF BLE - hardware design, firmware development, prototyping, PCB design/assembly, sourcing and manufacturing. We can help you bring your product to market!
Bluey nRF52 BLE IoT dev board
We love hearing from our readers. Email us at email@example.com for questions or comments on this article. If you found this article useful, please support us by buying some of our hardware products.