RC Truck and Construction  

Go Back   RC Truck and Construction > RC Tech section. > Electronics tech

Electronics tech Anything to do with the electronics in a model. Lights, Radio, ESC, Servo, Basic electrical.


 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-13-2018, 12:00 PM
Wombii's Avatar
Wombii Wombii is offline
Green Horn
 
Join Date: Feb 2018
Location: Norway
Posts: 231
Wombii is on a distinguished road
Default 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:
/************************************************************

For this example you can connect a wire from the signal pin on a channel on your receiver to the A0 pin on your Arduino.
Power the Arduino and receiver however you want, but make sure there is a ground wire between the receiver and Arduino.
The Arduino onboard LED should turn on (and flash) or off when flipping the switch on your transmitter.

************************************************************/

/*
read channels
simple switching and output
*/


//**Give your arduino I/O pins name tags**
// This is completely optional, but makes the program easier to read for humans.
//
// Pins available on most Arduinos. "*" means the pin can be used as a PWM pin for dimming LEDs. A0-A5 can also be called 14-19.
// RX  TX  02  03* 04  05* 06* 07 08  09* 10* 11* 12  13 A0  A1  A2  A3  A4  A5

// Radio pin aliases:
const byte channel1Pin = A0;

//**set up variables:**
//This is not optional. Variables are little boxes to store numbers in.

//Radio channel variables:
int channel1Value = 1500; //1.5ms (center) as a default value.




//This runs once at power on.
void setup()
{
    //set LEDpins to switch mode (output +5v or 0v)
    pinMode(13,OUTPUT);

    //Open the Serial port to enable sending data to the Arduino serial monitor or serial plotter.
    Serial.begin(115200);

}

//This keeps looping.
void loop()
{

    // --- Read channels --- 

    //This is the easiest way to read channels, but it's slow for more than 2 channels.
    //(One loop will take minimum 10ms, possibly 60+ms with 3 channels depending on receiver and the order you plug the channels in.)
        channel1Value = pulseIn(channel1Pin,HIGH);

    //Optional: Send pulselength of channel 1 to the Arduino serial monitor or serial plotter (115200 baud rate)
        Serial.println(channel1Value);


    // --- Convert channel values to logical switches --- 

    //Channel1 with 2 positions
        if (channel1Value < 1300)   //if the pulselength of channel 1 is less than 1300 microseconds
        {
            //The switch is off
            digitalWrite(13,LOW);     //turn the onboard LED on pin 13 off.
        }
        else                        //if the pulselength is more than 1300 microseconds
        {
            //The switch is on
            digitalWrite(13,HIGH);    //turn the onboard LED on pin 13 on.
        }

    
} //End of loop


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:
/************************************************************

For this example you can connect a wire from the signal pin on a channel on your receiver to the A0 pin on your Arduino.
Power the Arduino and receiver however you want, but make sure there is a ground wire between the receiver and Arduino.
The Arduino onboard LED should turn on or off when flipping the switch on your transmitter.

************************************************************/

/*
read channels
simple switching and output
*/


//**Give your arduino I/O pins name tags**
// This is completely optional, but makes the program easier to read for humans.
//
// Pins available on most Arduinos. "*" means the pin can be used as a PWM pin for dimming LEDs. A0-A5 can also be called 14-19.
// RX  TX  02  03* 04  05* 06* 07 08  09* 10* 11* 12  13 A0  A1  A2  A3  A4  A5

// LED pin aliases:
const byte LEDpin1 = 13;

// Radio pin aliases:
const byte channel1Pin = A0;

//**set up variables:**
//This is not optional. Variables are little boxes to store numbers in.

//Radio channel variables:
int channel1Value = 1500; //1.5ms (center) as a default value.

//Light function switch flags:
byte lightFunction1Switch = 0; //off as default value.

//Make variables for LEDpins:
byte LEDpin1State = 0; //defaults to off



//This runs once at power on.
void setup()
{
    //set LEDpins to switch mode (output +5v or 0v)
    pinMode(LEDpin1,OUTPUT);

    //Open the Serial port to enable sending data to the Arduino serial monitor or serial plotter.
    Serial.begin(115200);

}

//This keeps looping.
void loop()
{

    // --- Read channels --- 

    //This is the easiest way to read channels, but it's slow for more than 2 channels.
    //(One loop will take minimum 10ms, possibly 60+ms with 3 channels depending on receiver and the order you plug the channels in.)
        channel1Value = pulseIn(channel1Pin,HIGH);

    //Optional: Send pulselength of channel 1 to the Arduino serial monitor or serial plotter (115200 baud rate)
        Serial.print(channel1Value);
        Serial.print('\t');


    // --- Convert channel values to logical switches --- 

    //Channel1 with 2 positions
        if (channel1Value < 1300)   //if the pulselength of channel 1 is less than 1300 microseconds
        {
            //The switch is off
            lightFunction1Switch = 0;
        }
        else                        //if the pulselength is more than 1300 microseconds
        {
            //The switch is on
            lightFunction1Switch = 1;
        }

    // --- Logic stuff for the switches. ---
    // Note that this part doesn't actually write to the ports, but determines what should be written to the ports.

        //Light function 1 is a simple on/off switch for flasher.

        static unsigned long LEDpin1Time = 0; //Make a variable to store timing value for pin 3. 

        if (lightFunction1Switch == 1) //if switch is on
        {
            //Make light function 1 flash LEDpin1 on/off
            if (millis() - LEDpin1Time > 200) //If [current time] - [last time pin1 switched] is more than 200 ms
            {
                LEDpin1State = !LEDpin1State; //! means NOT or opposite. Set state LOW if it's HIGH and vice versa.
                LEDpin1Time = millis(); //store [current time] as [last time pin1 switched].
            }

        }
        else //if switch is off
        {
            //Turn LEDpin1 off
            LEDpin1State = 0;
        }

    /// --- Switch the Arduino I/O pins ---
    // digitalWrite(pin,state) where state can be HIGH/LOW or a value. 0 means LOW and anything else means HIGH.
    // analogWrite(pin,value) where value is a value between 0 and 255 can be used to PWM dim an LED on certain pins.

    digitalWrite(LEDpin1,LEDpin1State);


    //This section will send the current state of the LED pins to the "serial monitor" in the Arduino IDE.
    Serial.println(LEDpin1State);
    

    
} //End of loop
Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:53 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.