Introduction: Control Your RC Plane With Your Phone's Acclerometer
Have you ever wanted to control your RC airplane by means of tilting an object? I've always had the idea in the back of my head but I've never pursued it until this past week. My initial thoughts were to use a triple axis accelerometerbut then I had a idea. . .
Most of you have smart phones right? And another great majority of you electronic guys out there use Androids because of it's flexibility. I mean I love the iPhone, but it's not as versatile in the regards to connecting it via Bluetooth or hardwire to an Arduino or other microcontroller. So why not just the phone's accelerometer? So I took the idea and ran with it. Two weeks later I present you this project!
Sorry IOS users, this project only pertains to Android users.
Now this project has not control over the rudder or the throttle. If you'll notice I did add code for you to use a potentiometer on pin A1 to control the throttle, although I myself will not be using it. I'm just going to use my transmitter's throttle.
If you'd like to complete this project I suggest you watch the entire video before you start working. The electronic skills required for this project are minimal but the hard part is working with the code. So grab your soldering iron and Arduino and lets get going.
Project Cost: (Free, Low, Medium, Up there, Seriously?)
Project Difficulty: (Breezy, Easy, Medium, Hard, Expert, Master)
Project Time: (Over a cup of coffee, medium, over a pot of coffee, several days)
Below is a quick flight I had earlier when it was kinda windy!
Step 1: Watch the Full Video
The video and the text cover pretty much the same stuff except the text does not include the step by step process to building the project case and connecting everything. The text illiterates a tad bit more on some of the coding involved.
Step 2: IMPORTANT UPDATE [7/16/18]
I actually began to make this project a few years later and I had to back to this i'ble to get some information :D
Anyways I discovered that there is a little error with the code, and I'll show you fix that in just a few seconds. If you download the code, it will not compile. You must go in and change a small line.
When you compile, line 20 gives you an error.
int ppm[chanAmount];
Simply change it to:
int ppm[2];
And there you go! Enjoy the rest of the tutorial, and if you happened to make this project, scroll down to the bottom of the i'ble and click the "I made it" button and upload a picture!
Step 3: Ingredients:
What does every good recipe need? Ingredients. Unfortunately you might need to go somewhere other than your corner grocery for these.
Ingredients:
- Android Phone
- Arduino (Uno, Nano, Micro, doesn't really matter)
- Power source for the Arduino. I'm going to use a small 2 cell 500mAh LiPo.
- HC - 05 or HC-06 Bluetooth Module
- FTDI Breakout Board (can be done with the Arduino, but I won't be showing you how)
- 3.5mm Mono Male Audio Plug. A stereo jack WILL NOT work! I've tried!
- 2x LEDs and corresponding resistors
- Project Enclosure
- 2-core wire (speaker wire works great, or a long USB cable)
- Heat shrink or electrical tape
- Female Pin Headers (for the Arduino shield if you choose to make one)
- Perf board (for Arduino Shield should you choose to make one.
Tools:
- Soldering Iron
- Wire Strippers
- Scissors
- Drill with various drill bits
- Hot Glue Gun
Step 4: Downloads
If you'd like to download a .zip file of everything you will need for this i'ble you can find that here. Otherwise read below.
First of you need to download the Amarino app and the Amarino Toolkit and get it onto your phone. You could do this with a micro SD or just use your favorite file hosting website, like Google Drive.
Secondly you need to download the MeetAndroid library for the Arduino IDE. To install the library follow the steps carefully.
FOR PC:
- Close the Arduino IDE.
- Unzip the MeetAndroid folder. You should now have an unzipped MeetAndroid_4 folder. Open this folder and cut the folder inside called MeetAndroid.
- Navigate to C:\Program Files (x86)\Arduino\libraries and paste in the MeetAndroid. If you did everything correctly it should look like the first picture.
Reopen the Arduino IDE and you should see in the libraries under 'Contributed' the MeetAndroid lib.
FOR MAC
- Close the Arduino IDE.
- Unzip the MeetAndroid folder. You should now and unzipped MeetAndroid_4 folder. Open this folder and copy the folder inside called MeetAndroid.
- Navigate to the Arduino application, right mouse click show package contents and then Contents/Java/libraries and paste in the MeetAndroid folder.
- Reopen the Arduino IDE and you should see in the libraries under 'Contributed' the MeetAndroid lib.
Lastly download both my Arduino sketches that are attached to the bottom of this step (included in the .zip).
Attachments
Step 5: Configuring the HC-06
Your HC-06 Bluetooth module doesn't just come out of the box ready to use in this project. We need to change just 1 simple thing: the baud rate. Depending on who you buy yours from it may be set at different baud rates. I got two HC-06s from two different sellers, and each was running at a different baud rate. We need to change it to 115200 bits per second. Make the following connections:
HC-06 ------- FTDI Breakout Board
| -------------- |
GND -------> GND
VCC --------> 3.3v
TX ----------> RX (or DRX)
RX ----------> TX (or DTX)
- Plug the FTDI breakout board into your computer.
- Open the Arduino IDE. Select whatever com port is available. (make sure there are no Arduino's or anything else plugged into the computer).
- Open the Serial Monitor (no need for code!)
- Select 9600 for the baud rate at the bottom right of the com window. Type "AT" (it HAS to be caps) and hit send. If you don't get anything back that's fine. This means your device is configured at a different baud rate. Try a baud rate of 115200 and type "AT". If you still don't get anything try each and every baud rate and type "AT" on all of them until you get a response. If you don't get a response then most likely you haven't wired up the HC-06 correctly. Check connections.
- If you do get the response "OK" then awesome! If you get that response on any other baud rate then 115200, you need to change it to 115200. To do this type "AT+BUAD8". You'll get the response "OK115200". You will have to change the baud rate to 115200 to continue talking to the Bluetooth. Then you can do stuff like change the name by typing in "AT+NAMERANDOM" random being the name you want. You can now close the serial monitor.
Step 6: The Incomplete Circuit for Testing
This circuit is quite simple. You need to power the Arduino (through USB since we are going to need to upload some stuff to it), then connect the HC-06. Just connect follow the connections below.
HC-05/6 --------- Arduino
|=============|
GND --------------> GND
VCC ---------------> 5v
TX ----------------> RX
RX ----------------> TX
Step 7: Installing Amarino and the Amarino Toolkit
You should have downloaded and transferred your Amarino app and toolkit to your phone by now. But before you install it you need to change a setting on your phone. Your phone won't let you install the app at first because it is not a certified application from the Android playstore. So we will need to allow the phone to install un-certified apps. This is quite simple.
- Navigate to Settings/Security and scroll down. You should see a box that says Unknown Sources and a little box next to it. Tap on the box and enable. It will bring up a dialogue box telling you how risky this is; sounds good right? So tap that OK button. See pictures 1, 2, and 3.
- Now you can open your Amarino app and follow the onscreen instructions. Not too difficult just hit yes.
- Install the Amarino Toolkit. Not too difficult either.
Step 8: Setting Up Bluetooth
With your project powered (the Bluetooth chip is on) find your phone's Bluetooth settings. You need to pair your phone with the Bluetooth chip. Tap scan for devices and your device should show up as HC-05 or HC-06 unless you named it earlier. It will ask for a pin number, and it will 1234. (I know I'm a genius for hacking the device and finding the uber-secret password). See pictures 1, 2, and 3. Make sure that you are paired with the device and you are ready for the next step. NOTE: the light on the Bluetooth device will not go solid when you pair with your phone! I found that on my phone I had to restart it after this process for it to show up in the Amarino app, so go ahead and reboot your phone.
Step 9: Amarino
Time to open up Amarino. You will be presented with the release notes and tap OK. Thank you Bonifaz Kaufman for creating this wonderful app. The numbers below correspond with the picture number e.g. step 1 = picture 1.
- Welcome screen. Tap on "Add Device".
- Your device should now be in the list; tap on it. If it does not show up try rebooting your phone, and make sure that the Bluetooth device is connected to the phone.
- You should now come to this screen. Tap on "Connect". This may take a few seconds.
- If it connects then the light on the Bluetooth device will turn solid and you'll see at the top "Active Connections: 1. Now tap on the device name and NOT disconnect.
- Tap on "Show Events".
- There should be no events. Tap on "Add Event".
- A list will pop up and you will want to select the accelerometer. Keep in mind these other sensors that are built into your phone. They may come in handy one day for a later project!
- Here are just a few settings for the accelerometer. Make sure that the slider is on "very fast" and that the data visualizer is on "Text". Then tap save.
- If you see numbers to the right of the "Accelerometer Sensor" then you are good. You can leave your phone alone now. If you don't see numbers, long press on the gray area to bring up a menu.
- Select "Force Enable".
- Bingo! Numbers! Tilt your phone and get a feel for how the numbers correspond with the accelerometer.
- You can now place your phone on a flat surface and get ready to start some coding!
Step 10: Skipping the Code
So if you don't want to get your hands dirty with the code and you just want to upload and go you might be in a little bit of trouble. I've only used my phone so far so I don't know if other people phones are the same. If you download the app SensoDuino and click on the tab "Built in Sensors" you should be able to find your accelerometer. If you accelerometer is a MPU6050 then that is the same sensor that I have, and you probably don't need to do any code editing. If you have the same sensor, just try the code uploaded to this step.
If not then you will probably have to follow the next steps.
Attachments
Step 11: Assigning Accelerometer Data
Now we get to the fun part, messing around with the Arduino sketch! If your sketch doesn't compile see the last step of this instructable.
At this point you should now have your Arduino connected properly to your HC-06 and you should have Amarino pushing numbers out to your Arduino via Bluetooth. Double check to make sure that the HC-06 is still bound and upload the following sketch to the Arduino. (Attached to this step).
NOTE: When uploading sketches to the Arduino you will need to unplug the TX and the RX of the HC-06 from the Arduino. The Arduino can't be uploaded to while the RX and TX are connected to something.
After uploading this sketch to the Arduino open up the Serial monitor. You should see three numbers jumbled together separated by a box (□). Example: [ 0.01□0.02□□9.21 ] The first two numbers are roll and pitch. I didn't even try figuring out what the third one was :D. Your phone may or may not be similar to this. To find out which numbers are roll and which numbers are pitch are for your phone follow these steps.
- Set your phone on a flat surface. Orient your phone so that the camera/ear speaker is pointing to the left and the microphone is pointing to the right. (picture 2).
- Tilt the left side of the phone up (picture 3) (along the Z axis, see picture 1 for the axis) to 45 degrees. Note which number changes drastically in the serial monitor. This number will be your roll.
- Tilt the phone toward you (picture 4) (along the X axis, see picture 1 for the axis) to 45 degrees. . Whatever number changes drastically is pitch. Record this.
Now that we know which two sets of numbers are roll and pitch we can go and change the code. We will be taking a look at section 2.
pitch = data[0]; roll = data[1];
If you looked earlier in the code, you'll notice that the accelerometer data is put into an array. The array "data" has three 'spots' called 'indexes' for numbers, index 0, index 1, and index 2. Now that you know which numbers represent pitch and roll you can assign the integer "pitch" and "roll" to the correct numbers in the array. Example: if the third set of numbers changed when your tilted your phone left/right, which is roll, then we'd change the code to this:
roll = data[2];
And it's as simple as that. If you phone is similar to mine then the first number will be pitch, the second number will be roll, and the third number we be some other aspect of the three dimensions.
Step 12: Mapping the Accelerometer Data
Once you have "roll and "pitch" assigned to the correct numbers in the array data, we can move on. Look at picture 1 all the way on the left. This is the raw data from your accelerometer. But we can't use these numbers directly we need to remap them to a value between 1000 and 2000 with a midpoint of 1500. To scale the numbers up we will multiply "roll" and "pitch" by 1000. This will get rid of all the decimals and it will give us a decent resolution. Now if you look at the numbers in the second column you can see the 'new' numbers. Lets set some in and out points!
- Tilt your phone to the left 45 degrees (roll) and record this number.
- Tilt your phone to the right 45 degrees (roll) and record this number.
- Tilt your phone to the forwards 45 degrees (pitch) and record this number.
- Tilt your phone to the backwards 45 degrees (pitch) and record this number.
If the two numbers are around the same for when you tilted your phone 45 degrees in both directions you can ditch the second number and just use the first. Create a second number by taking the negative of the first number.
Example: Tilt phone 45 degrees to the right. The number show for roll is 5500. For the second number just use -5500. This will be our in and out points. We don't want our TX to register anything more than those points. Then we need to remap those values to 1000-2000. In my case -5500 to 5500 were the numbers that I got when pitching my phone 45 degrees and when rolling my phone 45 degrees. So I used the following code: NOTE: It is now time to delete the /* and */ in section 4 to enable this code. Also delete all of section 3.
pitchval = map(pitch,-5500, 5500, 1000, 2000) -12 ; rollval = map(roll, -5500, 5500, 1000, 2000) + 7;
Now the -12 and the +7 are adjustments to bring "pitchval" and "rollval" to a center of 1500. Make sure that your phone is on a flat surface when you make these adjustments. If your center is not at 1500 then your plane may veer off in a direction even though your have your phone level.
We need to constrain rollval and pitchval. We don't want them going anymore than 2000 or any less the 1000 or else things start messing up! So we type this. . .
pitchval = constrain(pitchval, 1000, 2000); rollval = constrain(rollval, 1000, 2000);
Last thing we need to do with these numbers is to make sure they correspond correctly with the way we tilt our phone. Check to see if your phone is face up with your camera on the left, and the microphone on the right.
- When you roll your phone to the left, rollval should decrease to 1000.
- When you roll the right, it should increase to 2000.
- When you pitch your phone backwards, pitchval should decrease to 1000.
- When you pitch your phone forwards, it should increase to 2000.
- Both centers should be at 1500 when you place your phone on a flat surface.
If when you roll your phone to the left and rollval increases rather than decreases, change the 1000 you multiplied "roll" by to -1000 (in section 2). The same goes for the "pitch" if the numbers are reversed.
Example code for fixing roll in the example above.
pitch = (pitch * -1000); // multiply by 1000 to get rid of the decimals roll = (roll * -1000);
So if everything is working fine then we can add this to the final code!
- Open the ReadRawAccel sketch. Copy everything under the page1 maker.
- Open the TiltTX_Final sketch. Scroll to the bottom where it says "paste page 1 here" and replace all code under it.
- Upload the TiltTX_Final sketch to your arduino.
Step 13: Circuitry Time!
Now that we are done with the hard part it is time to make the circuit. You may choose not to make a shield to save time and parts but I always like to make my projects with shields in case I need to use that Arduino with another project. For this project I will be using my Arduino Nano because I want this all to fit in a small box. But you could use any other Arduino, from a Mega to a LilyPad. So study the schematic, watch the video and start soldering! I won't post all the picture here because I show you step by step how to make it in the video.
Step 14: Setting Up the Trainer
Now that you are done with the circuit power it on, connect to your phone, and plug the mono jack into the training port on the back of the radio. Set the multiplier to 1.3 and place your phone on a flat surface. Then calibrate.
Note, channel 0 is throttle, channel 1 is roll, and channel 2 is pitch.
If you want to change these go into the code here:
ppm[0] = throttle; ppm[1] = rollval; ppm[2] = pitchval;
Step 15: Conclusion
Some people have had the issue of the code not compiling. Why I don't know because it work fine for me on both my platforms, but if you get the error messsage (error not defined in this scope) then you need place line 1 after void error loop.
1. MeetAndroid meetAndroid(error); //The Android bluetooth stuff. Don't touch this.
void error(uint8_t flag, uint8_t values) // more bluetooth stuff. { Serial.print("Error: "); Serial.println(flag); }
When you're done it should look like this:
void error(uint8_t flag, uint8_t values) // more bluetooth stuff. { Serial.print("Error: "); Serial.println(flag); } MeetAndroid meetAndroid(error); //The Android bluetooth stuff. Don't touch this.
I really hope you found this instructable interesting and fresh. I tried to make sure that it is easy to follow. Should you be confused with anything the video goes into a bit more detail then the instructable does. If you run into any trouble please contact me and I'll be happy to help as long as your question is relevant. Please give this instructable a vote and post a picture below to your replication, and if you made a video, post a link! Be sure to leave feedback below!
HavocRC,
~AJ