UChip - Simple Sketch to Remote Control Motors And/or Servos Via 2.4GHz Radio Tx-Rx!
by rantonio90 in Circuits > Arduino
1391 Views, 4 Favorites, 0 Comments
UChip - Simple Sketch to Remote Control Motors And/or Servos Via 2.4GHz Radio Tx-Rx!
I really like the RC world. Using an RC toy gives you the feeling that you are in control of something extraordinary, despite it being a small boat, car or drone!
However, it is not easy to customize your toys and make them do whatever you want them to do. Usually, you are constrained to use the default transmitter settings or the specifically designed combinations of switches and knobs.
Getting to control everything as you really want is quite hard, mainly because the RC world requires quite a deep knowledge of hardware-level programming in order to get the best out of it.
I tried many platforms and setups, but it always cost a huge effort in order to get comfortable enough with the code before doing some real customization to my RC toy.
What I was missing is a simple sketch that I could load using the Arduino IDE and that would easily allow me to translate the values coming out from the Radio RX (receiver) into the desired Motor/Servo control.
Therefore, here it is what I created after playing a bit with uChip and the Arduino IDE: A simple sketch to remote control Motors and/or Servos via 2.4GHz Radio Tx-Rx!
Bill of materials:
1 x uChip: Arduino IDE compatible board
1 x Tx-Rx Radio system: any radio system with cPPM receiver is good (my combo is an old Spectrum DX7 Tx + Orange R614XN cPPM Rx), make sure you follow the correct binding procedure in order to bind the Tx and Rx.
1 x Battery: high discharge current batteries are necessary when dealing with motors and servos.
Motors/Servos: accordingly to your needs
Electronic components to drive the Motors/Servos: simple resistors, MOSFETs and Diodes allow you to accomplish the driving purpose.
Wiring
Wire the components together as described in the schematics.
The Rx is connected directly to uChipand does not require any external componets. In case you are using a different receiver, verify whether you need a level shifter or not. Be sure to connect the cPPM signal to uChip PIN_9 (which is PORTA19 in case you want to adapt the code to another SAMD21 board).
The remaining wiring is necessary in order to drive the motor and/or the servo. The attached schematic represents the basic circuit in order to protect uChip from spikes/overshoots that usually occur when driving inductive loads. The key component to preserve uChip safety is the power Zener diode of 5.1V (D1 in the schematic) that you need to put in parallel to the VEXT (uChip pin 16) and GND (uChip pin 8). Alternatively, instead of using the Zener diode, you can opt for the optional circuitry represented by D2, C1 and C2, which prevents reverse spikes to damage uChip components.
You can drive as many motors/servos as you need by simply replicating the schematic and changing the control pins (you can use any pin except the power pins (PIN_8 and PIN_16) and the cPPM pin (PIN_9)). Mind that, while you need only one protection circuitry which is represented by the Zener diode (or the components for the optional circuitry), the electrical components related to the motor/servo driving has to be replicated as many times as the number of motors/servos you intend to drive.
Since I wanted to drive at least 2 motors and 2 servos, I made a small PCB which implemented the described circuitry and that you can see on the picture. However, the first prototype was made on a proto-board using flying wires.
Thus, you don’t need any soldering/PCB design skills to implement this simple project :)
Programming
Here is the magic! This is where things get interesting.
In case you built the circuit described in the previous schematic, you can simply load the sketch “DriveMotorAndServo.ino” and everything should work.
Have a look at the code and check how it works.
At the beginning there are few #define used to define:
- the number channels of the Rx (6Ch with the Orange 614XN)
- the pins where motors/servos are attached
- Max and min used for the servo and motors
- Max and min used for Radio channels range
Then, there is the variables declaration section where the motors/servos variables are declared.
In case you drive more than the one motor and the one servo attached as described in the previous schematic, you need to modify the sketch and add the code handling the additional motors/servos you attached. You need to add as many Servo, servo_value and motor_value as many servos/motors you are using.
Within the variables declaration section there are also some volatile variables used for the Capture Compare of the cPPM signal. DO NOT CHANGE THESE VARIABLES!
What you need to do next is in the loop() function. Here, you can decide what use to make of the incoming channels value.
In my case I connected the incoming value directly to the motor and servo, but you are more than welcome to change it accordingly to your needs! In the video and pictures linked in this tutorial I connected 2 motors and 2 servos, but there could be 3, 4, 5,...up to the maximum available free pins (13 in case of uChip).
You can find the captured channel value within the ch[index] array, whose “index” goes from 0 to NUM_CH - 1. Each channel corresponds to a stick/switch/knob on your radio. It is up to you to understand what-is-what :)
Finally, I implemented some debug functions in order to make it easier understanding what is happening. Comment/uncomment the #define DEBUG to print on the native SerialUSB the channels value.
TIP: There is more code below the loop() function. This part of the code is necessary in order to set uChip power pins, handle the interrupts generated by the capture compare feature, set the timers and debugging purpose. In case you feel brave enough to play around with registers, feel free to modify it!
Edit: Updated sketch, fixed a bug in the mapping function.
Downloads
Play, Drive, Race, Fly!
Make sure you bond correctly the Tx and Rx system. Power it up connecting the battery. Verify that everything works. You can expand the functionalities or change the function of each channel as you please, because now you are in full control of your future RC model.
Now, build your customized RC model!
P.S.: since binding can be quite boring to do, I plan to release soon a sketch that allows binding your Tx-Rx system without having to do it manually. Stay tuned for updates!