BLEBot - nRF51822 based BLE Robot12 Oct 2015
In this project, we will build a two-wheeled robot based on the Nordic nRF51822 BLE SoC. Motor control for the two wheels of the robot will be done using an L293D chip. The robot can be controlled using any mobile device that has BLE, and when not connected, it switches to an autonomous mode, avoiding obstacles using an HC-SR04 ultrasonic sensor.
Before you read further, you might want to look through some of my previous articles on nRF51822 programming, since we’re going to use similar concepts and 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)
- Motor Control over BLE with nRF51822 and TB6612FNG
Hardware & Connections
Here’s the list of hardware you need for this project:
- An nRF51822 Module.
- An nRF51822 Module Adapter (optional).
- L293D motor driver IC.
- Two wheel robot chassis with two motors.
- HC-SR05 ultrasonic sensor.
- A regulator IC like LM7805.
- A LiPo battery or similar which can supply enough voltage and current for the motors and the module.
The type of nRF51822 module I used is made in China, and available at sites like aliexpress, and they come with two 2x9 2 mm pitch headers. These are awfully hard to use, so I (in collaboration with my friend Sandeep) designed an adapter to make them breadboard friendly. These are optional for this project, but they are available for purchase on Tindie if you would like to check them out. In addition to making the module easy to use, our adapter also has a built-in LED and a voltage regulator.
Here’s how I hooked up the L293D and the nRF51822. (These connections are consistent with my code. If you do them differently, update the code to match.)
|P0.23||N/A||HC-SR04 Echo via resistor divider|
|N/A||4, 5, 12, 13||GND|
Note above that HC-SR05 runs on 5V, and nRF51822 on 3.3 V. So you need to ensure that the Echo signal from HC-SR05 is reduced from 5V TTL to 3.3V TTL, and the simplest way to do that is using a resistor divider, as explained in my previous article on using HC-SR04 with nRF51822.
For power supply, I used 4 x 1.5 V alkaline batteries. I supplied 5V to the L293D using a 7805 regulator, and since my nRF51822 adapter already has a built in regulator (and an LED), so I powered it separately from a 9V battery. I recommend that you use a good capacity 7.4 V LiPo battery to power this project.
nRF51822 Chip Versions & SDK
You need to ensure that the chip version, the Nordic nRF51 SDK, and the SoftDevice versions are compatible. The two documents you need to check this are the Product Anomaly Notice and the nRF51 Series Compatibility Matrix - both available from Nordic website or a web search.
From these documents, here’s how you identify the chip:
In my case, the chip on the module says QFACA2 and 1513AN. So it has 256 kB flash, 32 kB RAM, and it was made in 2015. This information is very important, and the ld file needs to be consistent with the chip. Here are the contents of my ld file:
Here’s how you identify the chip revisions:
And here’s a graphic that shows the SDK compatibility:
In my case, I am using the Nordic nRF51 SDK version 8.1.0 for this project.
I’ll go through the highlights of the code here, but you’ll need to go through the github link in Downloads below for the full picture. In addition to the Nordic SDK files, the code for BLEBot is organized in these files:
The Main Loop
Here is the main loop of BLEBot:
In the above loop, if the BLE connection is active, the robot follows instructions sent via the Nordic UART Service (NUS) from the mobile device. In this mode, it moves in reponse to left, right, stop, reverse, and start. I use the Nordic nRFToolBox app for using NUS. I also flash the LED connected to P0.21 twice quickly in each iteration of the loop.
Here’s the NUS data handler that takes action based on data that comes in via BLE.
In the above code, the motor states are changed based on the commands that come in. (These strings are tailored to the Nordic nRF Toolbox app, but you can change them to suite your mobile app.) It’s not a good idea to do any heavy lifting from BLE event callbacks, so all I do when I get an event is set a pending flag and the event type. The actual handling is done in the main loop, as you saw before.
Here is the data structure used for events:
The idea is that you can set a one-shot pending flag and the event type, which can be checked in the main loop and handled.
Here is the handler code for motion control.
The motion of the motors is controlled by sending the correct signal to the L293D (The D indicates that the IC has built-in flyback diodes.), which is configured as a dual H-bridge driver, controlling both the direction and speed of two connected motors. The speed is controlled by changing the PWM duty cycle on the A pins, and the direction of rotation is by setting the EN pins to HIGH or LOW. For example, this is how you set the direction of motion:
Above, we use the GPIO pins to set the direction of the motors. Here’s how you set the motor speeds:
To set speed, you just set the PWM duty cycle. And here’s how you turn:
To turn, you just reduce the speed of one motor for the specified time in milliseconds. And here’s how you stop:
The above methods are all you need to drive the robot around.
If BLE connection is inactive, the robot goes into an autonomous mode using the HC-SR04 ultrasonic sensor to detect obstacles. It uses a very simple algorithm: if it detects an object at a distance less than a certain threshold, it stops, reverses, turns left, and continues on. In this mode, I also flash the LED slower, so it’s easy to see whether the robot is connected via BLE or not.
Here’s the simple logic that is used to move the robot autonomously.
In the above code, if the distance from the ultrasonic sensor is less than a certain threshold, we stop the motors, reverse, turn, and then continue on forward. Nothing too fancy, but it does work. I have written previously on interfacing the nRF51822 with the HC-SR04 ultrasonic sensor, so I won’t repeat it here. The distance computation code is in distance.c.
Uploading Code to the nRF51822
To program the nRF51822 module, you need to use an SWD programmer. I can’t go into details here, but if you have the Nordic nRF51-DK, you can read my article on nRF51-DK external SWD programming to get started. You can use other tools to do the job also. For example, here’s an nRF51822 project that uses an st-link programmer.
Here’s BLeBot in action:
And here’s a view of some of the signals in BLEBot (Saleae Logic 8).
I think it’s quite impressive that this tiny Nordic SoC can handle an ultrasonic sensor, control two motors, blink an LED, and handle BLE communications at the same time.
You can get the complete source code for this project here: