Tech x Talent
Published in

Tech x Talent

Code behind a LED Light

When you learn any new programming language, first thing you do is print “Hello World”. This is a tradition and a nice way to learn the syntax of the language. I believe that an LED project is the hardware/electrical engineering equivalent to “Hello World”.

Components you need:



A breadboard enables us to prototype circuits without the need to solder connections. The connection either runs horizontally or vertically. The breadboard that I used had connections that ran horizontal, shown in red and blue lines in the picture above and vertical, shown in green lines. The way the connections are run are important when you connect your resistors or LED light wires. While breadboards are great to play with circuits, they are not as reliable as soldered connections, since breadboard connections are loose.

LED Light:

LED Lights

LED has a positive and negative lead. The longer side is the positive lead. The LED must be used with a resistor to limit or ‘choke’ the amount of current flowing through it, otherwise, it will burn out.



Simply put, resistors resist the flow of electricity. Unit of resistance is called the Ohm, which is shortened to Greek letter Omega. Usually, resistors have different colors on them that tell the developer the value of the resistor. Resistors do not have a positive and negative lead.

Wiring Diagram:

Wiring Diagram

Code to make a LED flash on/off:

// the setup function runs once when you press reset or power the boardvoid setup() {
// initialize digital pin LED_BUILTIN as an output.
// the loop function runs over and over again forevervoid loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(1000); // wait for a second

After connecting everything to the breadcrumb, the code above tells the board what to do. The code above basically turns the LED on for 1 second and then turns it off for one more second. This function is in a continuous loop. The digitalWrite is a function that takes in 2 parameters, LED_BUILTIN and HIGH || LOW . The loop basically takes in the LED, and then turns the volt to HIGH which turns it on then after 1 second, turns the same LED off by turning the volt LOW .

Turn LED on/off with button:


Expanding from the original LED light which turns on/off in an interval of 1 second, we can also turn the LED light on/off with a press of a button.

First, let’s look at the code:

int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
byte leds = 0;void setup()
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
void loop()
if (digitalRead(buttonApin) == LOW)
digitalWrite(ledPin, HIGH);
if (digitalRead(buttonBpin) == LOW)
digitalWrite(ledPin, LOW);

In this code, I have two buttons. buttonApin which is connected to 9 and buttonBpin which is connected to 8 . 9 and 8 is the physical place on Elegoo which the wires need to connect to. If the wire is placed anywhere else, we either need to change the initial state of the button to the place it’s in or change the wire to match the locations. This code looks at buttonApin , which is the button on the left side and if pressed, looked to see if the volt is set to LOW(if the LED is off). If the volt is LOW and we press the button, then the LED light will turn on. The button on the right, buttonBpin is coded in a way that it will turn the LED light off if pressed.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store