![]() |
Remote LED switching with Arduino examples.
I hope to one day write some getting started tutorials with Arduinos and RC, but for now, here are some example programs. You should already be comfortable with basic DC electronic circuits (don't short circuit or fry stuff) and very basic Arduino operations before starting here.
An Arduino nano is a nice cheap device (around 4 USD for Chinese clones) with 17 digital I/O pins. Each pin can be switched between 0v and 5v or be used to sense 0 or 5 volts in input mode. 5 of the pins can also be used as analog voltage sensors. Some of the pins can be switched with pwm, allowing you to dim an LED. You can try switching LEDs directly (remember resistors) as long as you keep below 20mA per pin and 200 mA in total. This is fine for a simple flashing circuit, but for more high power demands you will probably want to use transistors. Look into the ULN2003 transistor array to avoid having to calculate transistor base resistors (you'll still need LED resistors though). All Arduinos have an onboard LED that can be used for testing purposes on pin 13. The Arduino +5v pin can supply the Arduino with power or supply other devices if the Arduino is powered from another source. Don't supply your Arduino from a 5v source like a BEC through the +5v pin while at the same time having it connected to USB. This will be like connecting the BEC directly to your PC's USB port. Think about your connections before connecting power or things will start smoking. You can however safely power the Arduino from a source on VIN or dc jack while connected to USB as the Arduino will automatically connect its 5v rail to the higher source. Example 1: Switch onboard LED from RC transmitter and send the RC channel pulse length to a PC serial monitor to see how things work. In this example you can power the receiver from the Arduino +5v pin while the Arduino is connected to USB as long as you don't connect anything else to the receiver. Connect a wire from one of the signal pins on the receiver to pin A0 on the Arduino. It's a very short program, most of it is just comments for humans. This is enough to make brake lights or reverse lights. Code:
/************************************************************ Example 2: Same setup as example 1, but slightly more advanced. Now we flash the LED when it's turned on. The program is a bit more split up into modules to make it easier to add more functions for the next step. This could be a basic way of making simple turn signals or emergency flashers. Code:
/************************************************************ |
Re: Remote LED switching with Arduino examples.
Example 3: If you've made it here, please don't be intimidated. This is the same as example 2, but with 2 extra functions. 1 is a simple on/off. 2 is a simple flash/off. Function 3 is the fun one with a 3 position switch.
Position 1: marker. Position 2: marker + headlights. Position 3: marker + wig wag flashing headlights. Hopefully with some tinkering, you can see how these functions can form the basics of a light kit. Here you need to connect 3 channels from the receiver to the Arduino pins A0, A1, A2. You'll also need LEDs connected from pins 2, 3, 4, 5, 6 with appropriate resistors to GND. 1000 ohm should work fine for bright blues without blinding you or smoking anything. You can also skip the LEDs and just watch the Serial monitor output. Note that this setup is only for testing. If you connect anything else to this circuit like a servo, more LEDs or power the receiver from a battery or BEC, make sure you understand how much power goes where, and remove the wire from receiver to the +5v pin on the Arduino when connecting it to USB. Code:
//Wombii 2018 |
Re: Remote LED switching with Arduino examples.
Very nice start on a arduino tutorial Wombii. I've been wanting to learn more about the potential use of these boards in our hobby and I look forward to seeing more from you on the subject. Thanks.
|
Re: Remote LED switching with Arduino examples.
Thank you. The potential is endless and it's not too hard to learn if you find the right resource to learn from. Fortunately there are a lot of people out there writing guides in different ways. I remember learning the syntax felt overwhelming when I first got started, but it soon turned into a challenge learning how to think linearly and take one step at a time.
The reason I wanted to learn about this a decade ago was that I wanted to run multiple light functions and a 3 speed gearbox from a 3 channel radio. Not having enough channels seems like a common problem in this hobby, so let's see if we can build a function that helps with that. This method can be used to switch gears without a 3 position switch, switch turn signals by flicking the steering wheel twice, controlling all the emergency lights you could want or controlling double-tap style reverse lights. One way of attacking a problem is with pseudo code. Try refining it until each line only does a single action. Remember that the program loops about 50 times per second. This is what we want to happen from a human perspective: Code:
click switch x times Code:
if click detected Code:
if click detected Code:
if click counter is 1 Let's say we have a normal radio with a 2 position switch but no pushbuttons. Flicking the switch down then back up can be one click: Code:
if switch is up Code:
if switch is down Code:
if (channel1Value < 1300) Code:
/************************************************************ |
Re: Remote LED switching with Arduino examples.
Very cool.
How about some for what's needed to get started? Components, interface cables, and that kind of stuff (Taking notes from the back of the class) |
Re: Remote LED switching with Arduino examples.
I'll give it a shot :) Hopefully it won't be a complete formatting mess.
Strictly speaking, what you need to get started with an Arduino is the Arduino itself, a USB cable and install the free software on your pc. I recommend getting a genuine Arduino UNO / Genuino UNO to start with, as the cheap ebay ones have a higher dead on arrival rate, and it will help support the development of the software and hardware financially. When you've got the basics down, buy a drawer full of small Arduino nanos from China like me. If you're using windows 10 you can go to the windows store app and search for "Arduino IDE" https://www.microsoft.com/store/productId/9NBLGGH4RSD8 . Installing from the store will keep the app updated. Other install instructions can be found here: https://www.arduino.cc/en/Guide/HomePage . Here are instructions for uploading the first sketch to an Arduino UNO: https://www.arduino.cc/en/Guide/ArduinoUno And here are instructions for uploading the first sketch to an Arduino Nano: https://www.arduino.cc/en/Guide/ArduinoNano . If you bought cheap off brand nanos, you may have to select Atmega328p (Old bootloader) in the processor menu to get it to upload and you may have to install some drivers for the USB chip. After getting the first blink example to work, look through the other examples and keep https://www.arduino.cc/reference/en/ close by. That will be your favorite site for a while. *** For the people towards the front of the class: If you have any experience with C or C++, you may notice that you feel right at home. The basic Arduino is an Atmega 328p microcontroller and a USB-serial converter on a pcb. If you want to use AVR C examples from Atmega or school, with int main() instead of setup() and loop() or PIND instead of digitalRead(), those can usually be pasted right into the Arduino environment. Arduino just adds some premade functions and clock setups behind the scenes to simplify the coding. *** There are many getting started kits out there, but I would recommend getting the following as a minimum, in a prioritised list (The links to banggood.com are only meant as examples): - Breadboard, just push the components in and make your circuit. https://www.banggood.com/Prototype-B...-p-948106.html - Dupont jumper wires, can be used with breadboard and arduino or you can even plug LEDs straight into them. Get all three types, female-female, male-male, male-female. https://www.banggood.com/120pcs-20cm...-p-974006.html - An assortment of resistors. The blue 1/4 watt 1% is very cheap, but the leads are a bit thin. https://www.banggood.com/Wholesale-6...e-p-53320.html - LEDs. Standard 5mm of any color will do. https://www.banggood.com/375pcs-3MM-...p-1027601.html - Pushbuttons. Often called "PCB tactile push button". Make sure they have legs. https://www.banggood.com/100pcs-Mini...-p-917570.html - Digital multimeter. Being able to measure voltage and resistance will help you a lot. You probably don't need to go expensive for 5 volt electronics. - 22-24 AWG solid core wire to make your own smaller jumper wires for the breadboard - Wire strippers - Wire cutters Some programming tips: Looking at https://www.arduino.cc/reference/en/ you may feel a bit overwhelmed. Remember that all those functions are there to help you! You don't need to learn all of them. Here are some info on the basics: The first thing that confused me was whitespace. Whitespace like spaces and indents and linebreaks doesn't matter, they're just there to make it look prettier. The only time a line break matters is to end a //comment. FALSE and LOW is the same as 0. TRUE and HIGH is the same as anything else. This works for both tests and I/O functions. A variable is a box to store numbers in. You can make them the size you want and call them (almost) whatever you like. Byte is a 1 byte variable, storing a value up to 255 when you want to save memory. Int will be your standard workhorse, 2 bytes stores up to 32768. Long stores 4 bytes for a value of 2 billion. Variable1 = 23; saves the value 23 in the variable Variable1. Variable1 = Variable1 - 3; Makes the saved value 20. There are two basic control statements: if…else and while. Switch…case is series of if…else statements and for can be done with a while loop. If ( thing1 > thing2 )If the test condition inside the parenthesis is true or is more than 0, the instructions inside the curly brackets are performed. The test conditions can be math, a single variable or a test like <, >, == (is equal to), != (not equal to), >= (larger or equal) and <= (smaller or equal) with multiple variables or values. Else if and else are optional and will immediately follow the if statement. Else will do its thing if the other test conditions are false or 0. While will loop inside the curly brackets as long as the test condition is true or more than 0. This is a more useful while loop that can be replaced with a "for" statement. This example will delay for 10 x 1000 milliseconds. Sometimes you'll see these control statements without curly brackets. The curly brackets are optional if there is only one single instruction in the branch, but skipping them can cause human confusion. Digital in and out: The I/O pins can be internally switched to 0v (LOW) or 5v (HIGH). They can also be disconnected or floating. The behaviour is switched with pinMode() and digitalWrite(). digitalRead() can read the state of the pin. pinMode(pin number, mode); All pins default to INPUT and LOW if not changed.mode OUTPUT: Use a series resistor for all connections when setting pin mode to OUTPUT.mode INPUT: * mode INPUT_PULLUP: INPUT_PULLUP is a shortcut for setting pin mode INPUT and digitalWrite HIGH. |
Re: Remote LED switching with Arduino examples.
Thanks for very good information and code.
I am new to Arduino but done some other programming with HTML and PHP. Think I got everything working, but now I want to add my throttle channel from the radio and use it for reversing lights? Can someone help with a piece of code for when channelX goes below YY value it turn on LEDpinZZ |
Re: Remote LED switching with Arduino examples.
We can absolutely get that working! Did you test the first example in the first post? That example should turn on the onboard LED at pin 13 when the channel goes below 1300 us. You can change that to 1500 for a standard center value or change < to > if the throttle channel is reversed. You can also use the serial monitor with that example to read out what the program registers as the channel value at any time to find your specific center.
If that does what you want, that's great. If you want it to also understand the difference between braking and reverse we need to complicate things. If this is not what you were asking for, let me know :) |
Re: Remote LED switching with Arduino examples.
I have put a couple of hours into this and now got everything working.
Even extended with reverse sound and light.. Beeping when reversing. |
Re: Remote LED switching with Arduino examples.
That's great! Sorry for missing your post all last week.
Here's the relevant parts of my program that estimates when the ESC is in brake or reverse mode. Can be configured to work for double pump reverse or delayed reverse depending on the type of ESC. Code:
// - Global variables - |
Re: Remote LED switching with Arduino examples.
Just stumbled across this thread. For the last couple of weeks I've been working on programming an Arduino Nano to do something similar. I'm working on an R/C conversion of a Bruder Manitou 2150, with a few compromises and an Arduino I think I can get the job done with 6 channels. I'm going to have to study your programming and see what ideas I can shamelessly borrow.
Don |
Re: Remote LED switching with Arduino examples.
That sounds fun! Good luck, and let me know if there's anything I can help with.
|
Re: Remote LED switching with Arduino examples.
Wombii:
My plan was to monitor the steering and throttle/direction channels with the Nano. From the pulse widths of those two channels I was going to decode the various LED functions that I wanted to use. These are the various lights that the Nano will control on my Manitou without using any additional channels: Headlights/tail-lights - If we're moving turn these on. Work-lights - If we're not moving, or just moving slowly turn on the work lights. Back-up lights - If we're in reverse, pulse length less than 1.5 milli-seconds, turn on the back-up lights. Front Turn signals - If we're turning the appropriate front turn signal will flash on for 250 milli-seconds and then off for 750 milli-seconds. Rear Turn signals - These do double duty as turn signals and brake lights. If we're turning and not braking they flash in sync with the front turn signals. If we're turning and braking then they are off when the front turn signal is on and vice-versa. Brake lights - When the average of the speed set-point is decreased, then we're braking - turn on the brake lights for a few seconds. I've got a timing glitch in the rear turn signal programming and it doesn't always do what I want it to, but I'll find the little bugger and fix it. I haven't got the R/C channel decoding logic written yet. I was going to use the pulsein function, but everything I've read says that I should be using interrupts instead. I need to look into interrupts and then get the R/C decoding logic written next. Don |
Re: Remote LED switching with Arduino examples.
Sorry, I didn't see your last post until today.
That's a great project to work with the arduino. Seemingly simple on the surface, but can be made terribly complex as you work to perfect each function. Post #4 above contains an example that could be used to switch turn signals or work lights by flicking the steering a number of times left or right. I'd say that unless you run into timing critical things that absolutely can not hang or crash, and are reading 3 channels or less, just go with pulseIn. For a lightkit you probably don't need to do stuff anyway without having got an update on the RC channel data. You just need to be aware that the pulseIn way may in the worst case scenario need 2 frame times (5/11/16.5/20 ms) to read each channel depending on the order the receiver outputs them. If your receiver updates each channel every 20ms, you might for example read throttle pulse, miss steering pulse, wait 20ms for the next steering pulse, then the same for channel 3, meaning your arduino program only loops every 60ms instead of 20. If this gives you an issue, I have made a bit of code that always reads 3 channels in one cycle but sacrifices a bit of accuracy. Another option is to only read one channel in each loop. If you want to read more than 3 channels with pulseIn or the looptimes end up longer than your light flashing intervals, you can probably work around most issues by changing the order you're reading the channels in. (One of my receivers output ch1 and ch2 at the same time before ch3, so reading ch1-ch3-ch2 instead of ch1-ch2-ch3 saved me 20 ms). For my fire truck project I'm using the SBUS protocol to read 16 channels. You may want to have a look at Open Source Lights, an arduino based light controller. I don't use it personally as I started my project long before I found it, but it's very good (though complex to read through because of all the configuration options). I tried to help by contributing my light flashing+fading code in version 3, but some people have had problems with it, so you might want to look at version 2. It uses pulseIn, but with some error checking. V3 reads only one channel per loop to make sure it gets a stable loop time, because it uses loop counts instead of millis() to flash the lights. |
Re: Remote LED switching with Arduino examples.
Wombii:
The delay is OK. My day job is in engineering so the Christmas and July 4th plant shutdowns are my busiest times of the year. This is the first time I've been able to get back to this. I've talked to a member of another forum and he gave me a lot of info regarding interrupts. Because of him I found the FAST SERVO library which has a TON of good stuff in it. Hopefully I can get back to this in another couple of weeks and make some more progress. Don |
All times are GMT -4. The time now is 06:43 AM. |
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.