this tutorial is more focus to programming part of Atmega chip using Arduino programming language.
My goal has been to created railroad signal for my son trains.
- Changing between green and red using button.
- Changing between green and red automatically after some period of time.
- Build the chassis from LEGO.
- Program it on Atmega chip to learn the Arduino language.
Step 1: What We Need...
- red LED 3mm
- green LED 3mm
- resistors - 150 OHM and 140 OHM (depends on LED Voltage and Current)
- USB Nano V3.0 ATMEGA328P CH340G 5V 16M Micro-Controller Board For Arduino (or Arduino Nano or compatible)
- some wiring
- clicking button
- soldering station
- tape, double side tape
- optional 5V battery
- optional LEGO pieces
Step 2: Wiring
I decided to use PWM output pins to make the LEDs fading when the LED is on. Of course, you can use standard output to on/off LED. But in my case, I want to change the brightness of led smoothly.
In my case I have 6 PWM ports: D3, D5, D6, D9, D10, D11. I use D5 for red LED and D6 for green LED.
Because I will use INPUT_PULLUP button I don't need resistor on the button ground, because I will use internal one.
It is also good to calculate good resistor for your LED.
My red LED is 2,0 V and 20 mA. Because we connect it to 5 V and LED is 2 V the rest Voltage is 3 V. This is mean that for full brightness of LED we will use 150 Ω resistor because R = U/I = 3/0,02 = 150 Ω.
For my green LED is 2,2 V and 20 mA. We will use 140 Ω resistor because R = U/I = 2,8/0,02 = 140 Ω.
You can use also higher resistor to control a brightness of the LED.
Step 3: Programming
Here is my Arduino code. You can modified the code as you wish. I put comments into the code, so the program should be clear.
I was inspired with these tutorials and combine them together:
Step 4: Assembly
For assembly the railroad signal I've use good old LEGO. This was also the reason I've used 3mm LEDs. Because these fits into one special LEGO piece, so I can put into it.
For holding a button I have used double side tape. For back side of railroad signal I use tape to cover the back side wiring.
If you want to handle also a power do not forgot to add 5V battery and connect it to VIN and GND. But I've used power from mini-USB port using power bank connected to it.
Step 5: Video