Introduction
I recently bought a Turnigy Trooper SCT 4×4 1/10 Brushless Short Course Truck from HobbyKing.com and decided I wanted to do a bit of night time racing. For this projects I wanted some led’s and to round it off I wanted to use a Micro-Controller add extra lighting effects. For this project I picked the Atmel ATTiny84 micro-controller and I will use its PWM capability to control the led’s.
Goals I would like to achieve from this project.
- Switch on LED’s from my RC transmitter.
- Fade the LED’s with PWM
- Control Left and Right Side LED’s individually with the MCU.
- Finally , run it all from the RC’s battery.
Bill of materials
- ATTiny84 MCU – Datasheet – Digikey
- 4 x Super Bright LED’s – Datasheet – Digikey
- 4 x 100 Ohm Resistor(Led Current limiter) – Digikey
- 5V Power supply(Optional) – Overview
Build
Goals:
- To be able to switch on the Led’s from my RC transmitter I will have to intercept the signals with the ATTiny84 MCU and turn them into useful values. Most RC car receiver and transmitter kits these days consists of 3 channels. 2 channels are for steering and speed control. The third one for extra assemblies. The link below from Skarkfun was a huge help. It showed how to link to a receiver to an Arduino and how to measure the pulse lengths of the signals. From that I concluded the following:
- if the pulses were more than 1000 hertz it indicated that the 3rd Channel switch on my transmitter was in the off position
- if the pulses were less than 1000 hertz the button was in the on position.
- If the pulses were below 100 hertz it indicated that the transmitter was not receiving signals.
- Fading the LED’s was very simple. The MCu has 2 PWM channels connected to 4 pins (Pins 5,6,7,8 as per image below). By sending the pins a value from 0 to 255 you can set the brightness of the led’s and by using a few coding loops you can create some nice effects.
- Used PWM pins 5 and 6 to control the left and right side of Led’s.
- Added a 5V regulator to be able to use the RC’s on-board battery as an power supply.
Lessons learned:
- The ATTiny84 Pin’s are numbered diffidently in the Arduino IDE than on the datasheet.
- Arduino Pin 0 = MCU Pin 13
- Arduino Pin 1 = MCU Pin 12
- Arduino Pin 2 = MCU Pin 11
- Arduino Pin 3 = MCU Pin 10
- Arduino Pin 4 = MCU Pin 9
- Arduino Pin 5 = MCU Pin 8
- Arduino Pin 6 = MCU Pin 7
- Arduino Pin 7 = MCU Pin 6
- Arduino Pin 8 = MCU Pin 5
- Arduino Pin 9 = MCU Pin 4 (Reset Pin)
- Using a 7.2v battery the 5V regulator can switch off at times if the voltage fall’s below its operating voltage. This can be fixed by using a more expensive DC-TO-DC converter.
- At watchdog needs to be added to the code. This will be useful to keep the MCU running at all times even when the battery voltage gets unstable and makes the MCU freeze up.
- Using an Arduino Board as a programmer works fine, but it would be nice to have a SPI plug that I can just plug into my MCU board for programming.
Photo’s:
Arduino Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
// RC Receiver int ChlIn = 6; // 6 int ChlInValue = 0; // LED Channels int ChlR = 7; // 7 int ChlL = 8; // 8 // On or Off state boolean IsOn = false; void setup() { pinMode(ChlIn, INPUT); pinMode(ChlR, OUTPUT); pinMode(ChlL, OUTPUT); } void loop() { int CheckInput = pulseIn(ChlIn, HIGH, 25000); if(CheckInput < 1000) { if(CheckInput < 100) // Receiver Switched off { if(IsOn) PowerOff(); IsOn = false; } else // Switch Lights on { if(!IsOn) StartLEDS(); IsOn = true; } } else { // Switch Lights off if(IsOn) StopLEDS(); IsOn = false; } } void PowerOff() { for(int K=0;K<10;K++) { for (int a=255; a>=0; --a) { analogWrite(ChlR, a); analogWrite(ChlL, a); delay(1); } } } void StopLEDS() { for (int a=255; a>=0; --a) { analogWrite(ChlR, a); analogWrite(ChlL, a); delay(10); } } void StartLEDS() { for (int a=0; a<=6; a++) { digitalWrite(ChlR, HIGH); // set the LED on digitalWrite(ChlL, LOW); // set the LED off delay(150); // wait for a second digitalWrite(ChlR, LOW); // set the LED off digitalWrite(ChlL, HIGH); // set the LED on delay(150); // wait for a second } digitalWrite(ChlR, LOW); // set the LED off digitalWrite(ChlL, LOW); // set the LED on delay(500); for (int a=0; a<256; a++) { analogWrite(ChlR, a); analogWrite(ChlL, a); delay(1); } delay(100); for (int a=255; a>=0; --a) { analogWrite(ChlR, a); analogWrite(ChlL, a); delay(1); } for (int a=0; a<256; a++) { analogWrite(ChlR, a); analogWrite(ChlL, a); delay(2); } } |
Useful links:
Sparkfun Tutorial on interfacing RC receivers with an Arduino Board
This is very cool, my friend !
Pingback: RC Night Time Racing (RC Car + LEDs + ATTiny) « scrapydo