Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple arduino code to handle pwm and drive rc car headlights (LEDs) from a receiver (power and signal)
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
To upload to your Gemma or Trinket:
1) Select the proper board from the Tools->Board Menu
2) Select USBtinyISP from the Tools->Programmer
3) Plug in the Gemma/Trinket, make sure you see the green LED lit
4) For windows, install the USBtiny drivers
5) Press the button on the Gemma/Trinket - verify you see
the red LED pulse. This means it is ready to receive data
6) Click the upload button above within 10 seconds
byte PWM_PIN = 3;
const int ledPin = 13;
const int headLights = 6;
const int lowBeams = 60;
const int highBeams = 255;
int rxval;
void setup() {
pinMode(PWM_PIN, INPUT);
pinMode(ledPin, OUTPUT);
// Serial.begin(115200);
void loop() {
rxval = pulseIn(PWM_PIN, HIGH);
// Serial.println(rxval);
if (rxval >= 990 && rxval <= 1450)
//low value
analogWrite(headLights, LOW);
digitalWrite(ledPin, LOW);
else if (rxval >= 1451 && rxval <= 1850)
//medium value (middle)
analogWrite(headLights, lowBeams);
digitalWrite(ledPin, HIGH);
else if (rxval >= 1851 && rxval <= 2250)
//high value
analogWrite(headLights, highBeams);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment