Introduction: Bluetooth RC Car With STM32F103C and L293D -- Inexpensive
I made a Bluetooth Arduino Car like the one depicted here, by Ardumotive_com. The problem I was having was batteries and their weight as well as their expense. Since then, cheap power banks for cell phones have become very affordable. All I needed to do was to reduce the weight. Being that I'm cheap, I switched to the STM32F103C Microcontroller. The STM32F103C Microcontroller can be bought for less than $2, and is much smaller than an Arduino. I changed a bit of the coding to work with the STM32F103C as well.
- A cheap remote control car that eats batteries. Yes, just like the one that Ardumotive_com uses. You'll switch out the system and use a phone power bank instead. If you have the resources to build your own chassis, have at it. I went to the toy store down the street and bought a cheap car for under $10. The car eats batteries, and the remote eats batteries--perfect for improvement.
- A phonepower bank-- They are super cheap, now. Stray from the power banks that have a power button on the side. You won't be able to follow your car and hold the button. That's silly.
- A L293D Chip-- This is the dual H-Bridge that will control the electric motors.
- An HC-06 Bluetooth Module
- A Switch-- I used a simple on/off switch.
- Some Wire --phone wire would be fine, but a little larger stranded 20 gauge would be better.
- A Proto Board or a piece of plastic or cardboard to mount your Blue Pill and L293D. I'm cheap so I came up with a little different system with thin corrugated cardboard-- like from a light bulb box.
- Two Cheap USB charging cables--These can be purchased from a Dollar Tree. Don't use your nice programming cable. One will be cut for the on/off switch, and the other will charge the battery.
- 4 LEDs --if you want headlights and taillights.
- 4 220 Ohm Resistors-- for the LED s on a 3.3v system.
- A piezo or small speaker for a horn.
- Soldering Iron and Solder
- Hot Glue Gun-- My daughter is a Hot Glue Gun Ninja!
- Wire strippers and snips
NOTE: if you use the cardboard technique that I'm using rather than a proto board, you will need a Dremel or small drill.
Step 1: Destroy the Battery Eater!
It's time to have fun destroying the battery eater! Yes, GUT THAT THING! Feel proud that you are doing your part in making the world greener-- OK, that's a stretch, but anyway... Get to the frame.
Above, is the same unit that I made the Arduino version. The Arduino version used serious battery power that made the car heavier. So, I took it back down to the frame. I had added some fenders from a plastic bottle and hot glue, and customized the body. More on the body later.
Once you have the frame with the motors and steering bare, find which side of the motor terminals are which. Use a battery or 5v charger to test the motor.
On the steering motor, when the wheels turn right, label the positive battery wire "3" and the negative wire, "6".
On the driving motor, when the wheels rotate forward, label the positive battery wire "14" and the negative wire "11".
Step 2: The Code in Arduino IDE
It may be best if you prototype your car's electronics first on a breadboard.
OK, this is one of the tricky parts. The "Blue Pill" can't be programmed through the USB port. I haven't found an easier programming explanation of the "Blue Pill" than Joop Brokking's Youtube Video. It explains everything you need to know including Roger Clarke's STMduino library. There is a way to install a bootloader so you CAN use the USB to program the "Blue Pill," but you have to program the bootloader through the Serial Bus anyway.
Unfortunately, the Serial Bus is used by the Bluetooth adapter as well. The program must be installed through the Serial Bus , PA9 and PA10 pins, via a FTDI first, then you can check all of your settings with the Bluetooth adapter.
Use a breadboard and layout everything on the breadboard just like the fritzing sketch above. Disconnect the Bluetooth adapter's Serial TX and RX lines on the STM32F103C's PA9 and PA10 pins. Hook in your FTDI and program. Make sure that the Serial Bus lines are crossed, RX to Tx and Tx to RX. One receives and the other gives.
Once the program is loaded, you can open the serial console and send
to see if the lights are working. If the lights are working, you can send
again to turn them back off.
Put your car on a block to elevate the tires and send
The wheels should go forward. If they don't, reverse the wires. Remember how we labeled the wires earlier. The corresponding pins of the L293D should be matched.
To stop, send
Let's look at the significant changes in the code.
In the commented section, beginning, you should see the files originator, from Ardumotive. The next few comments explain where I've changed a bit to reflect the STM32F103C.
/* * Created by Vasilakis Michalis // 12-12-2014 ver.2 * Project: Control RC Car via Bluetooth with Android Smartphone * More information at <a href="http://www.ardumotive.com" rel="nofollow"> http://www.ardumotive.com </a> * * Changed this code to fit STM32F103 by Jim Garbe, firstname.lastname@example.org * More info at <a href="https://github.com/jgarbe/RCCAR_STM32F103C" rel="nofollow"> https://github.com/jgarbe/RCCAR_STM32F103C
</a> * Note that 8-bit values 0-255 have been changed to * reflect 16-bit values 0-65535 */ /*************************** * On the STM32, the analog write still works at 8-bit 255, * But you can get the full functionm of the PWM range, 0-65535, by declaring the Pin as PWM *AND using pwmWrite() instead of analogWrite() ****************************/
Most notably, the pins are not named in the same way between the Arduino and the STM32F103C. We declare the pins using the next set of lines. There is one pin left that is declared way down in the loop. On line 197, PA5 is used to read the battery level.
////L293 Connection const int motorA1 = PB6; // to Pin 15 of L293 const int motorA2 = PB7; // to Pin 10 of L293 const int motorB1 = PB8; // to Pin 7 of L293 const int motorB2 = PB9; // to Pin 2 of L293 //Leds connected to STM32F103C Pin A12 const int lights = PA12; //Buzzer / Speaker to Arduino UNO Pin A8 const int buzzer = PA8 ; //Bluetooth (HC-06 JY-MCU) State pin on pin A11 of STM32F103C const int BTState = PA11;
Also, using analogWrite(); will still work on the "BluePill". But it is better to declare the PWM pins using,
pinMode(<pin name> , PWM);
pwmWrite(<pin name>, <0-65535>);
NOTE: 8-bit = 0-255, 16-bit=0-65535
Lines 32-44 are changes made to the battery. If you are going to use the battery level checking, you must use a votage divider for the battery you have. This part is not reflected in the Fritzing sketch. There are plenty of explanations on how to create a voltage divider on Youtube. Because the STM32F103C is a 3.3v chip, I fixed the code here to physically use a voltage divider. The Arduino can tolerate some higher voltages through the ADCs provided but the "Blue Pill" can not.
/* Battery level will be checked on Pin PA5 * Changed the next line for the STM32F103C because the ADC can't handle * anything over 3.3v * I just commented it out * A voltage divider, using two resistors must be calculated and used * to measure the ADC input further down in the code * example: * GND---2K resistor-----------------1K resistor ------5v * | * | * 3.3v */ // const float maxBattery = 3.3; // Change value to your max battery voltage level!
Step 3: Put It All Together
I usually use a proto-board to place the pieces and solder between the holes to connect everything. Sometimes I "deadbug solder" everything together for more of a Frankenstein/3D wad of solder look.
I chose this hybrid method to make the device clean and lightweight-- and of course, CHEAP!
This method allows for labeling as well. One of the worst parts of deadbug soldering is when you look at an IC chip from the bottom and forget which pin is what.
The pictures above are kinda self explanatory. I guess the hard part is finding the thin enough cardboard to dimple and be rigid at the same time. You can use plastic as well but marking it is a little harder. Once I press the pins to the board and mark the dimples, I use a Dremel to drill each pin hole through.
If you haven't already noticed, I only have the lights as an accessory plug on the board. I'm not using the battery indicator, nor the beeper. It's because my project is for a different purpose. It will be self-explanatory once you see the finished result with car body. ...but this brings another idea, There are a lot of unused pins on this project. Maybe a trunk opener, car door opener, fire-cracker detonator, ... ...or even a mini- Galvani-Edison Luminiferous Aether Disturbance Generator!
Once all of the soldering is completed, test before you hot glue the joints for stress relief on the wires.
I used the same Android App as Ardumotive, It can be found at https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller
Once you have tested the car's functions, It's time to place the battery and switch. Go to the next step.
Step 4: Battery and Switch
OK, this is where you can't follow my plan exactly.
Somehow, you will have to find a good place to put your battery on the car with either, a way to charge the battery bank from a dongle, or a way to directly charge the battery plug. In the introductory video, I just taped the battery and microcontroller to the frame and ran it. When I wanted to stop, I just unplugged the battery. The problem with this setup is the perishability of the plugs on your USB cable and/or your power bank. It's better to have a switch.
You will also have to find a good place for the switch where the car body will still allow access. I used a plain push button switch (not a momentary switch), and mounted it on the bottom of the frame where the original battery compartment is located.
You'll have to cut a USB cable in half and put the switch between the battery and the STM32F103C USB port. Yes, you can power the STM32F103C with the USB port. You just can't program it through the USB port. I used a Dremel again to drill some holes for the switch soldering pins. Once soldered, I used Hot Glue, again for reinforcing the connections.
Step 5: Put Your Car Body on the Frame
OK, I said that I re-purposed the original Arduino version of this car. The actual final product, then, was a stage prop for "The Nutcracker" ballet performed by our local ballet company. In the opening scene, a mouse ran across the stage with Drosselmeyer's accidental magic. I used an IKEA rat and fitted it on top of the frame, Arduino, and much larger battery pack. The prop was heavy and not rechargeable. This is much better!
Have fun with your car. Remember that there are many more pins on the STM32F103C that can be used. Maybe a skunk similar to the one in "Toy Story 4."