Skip to main content

How to Blink LED Lights in Binary Pattern with Arduino

Learn how to use four different colored LEDs

In a faraway kingdom, there was a powerful wizard who could control the elements with his magic. 

A picture of a wizard heading into a faraway castle

He had four special orbs that glowed with the colors of the elements: red for fire, blue for water, green for earth and yellow for air.

A picture of a wizard with glowing orbs around

The wizard had discovered a secret language that only the wisest of creatures could understand. It was called binary and it used only two symbols: 0 and 1. With this language, the wizard could communicate with the spirits of the elements and command them to do his bidding.

A picture of a wizard with 0s and 1 around him and a book


One day, the wizard decided to create a spell that would make his orbs glow in a special pattern. He wrote the spell using a magical code that looked like this:




int yellowLED = 7;
int redLED = 6;
int blueLED = 13;
int greenLED = 11;
int dit = 100;
int dat= 500;

These lines declare variables that will be used in the program. The variables yellowLED, redLED, blueLED, and greenLED are set to specific numbers that correspond to the pins on the microcontroller that will control the LEDs. The variables dit and dat represent the duration of time that the LED will be on and off.

void setup() {
pinMode(redLED,OUTPUT);
pinMode(blueLED,OUTPUT);
pinMode(greenLED,OUTPUT);
pinMode(yellowLED,OUTPUT);
}

This is the setup() function, which runs once when the Arduino board is powered on or reset. These lines set the pins for the LEDs as output pins, meaning that they will be used to send a signal (in this case, to turn the LEDs on and off).


void loop()

This is the loop() function, which runs repeatedly after the setup() function has completed. These lines define the binary patterns for each of the 16 possible combinations of LED states, in Morse code. The digitalWrite() function is used to set the state of each LED (either HIGH or LOW).

digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//0000
This section of code is part of the main program loop that runs continuously until the microcontroller is turned off or reset. It first turns off all of the LEDs by setting their pins to LOW, then waits for a duration of time specified by the dit variable. 
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//0010
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//0011
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//0100
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//0101
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);
//0110
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//0111
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//1000
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1001
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);
//1010
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1011
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//1100
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1101
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//1110
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1111
}

After this delay, it turns on the first LED by setting its pin to HIGH, waits again for dit time, and then moves on to the next LED. This pattern continues until all of the LEDs have been turned on and off once, and then there is an additional delay of dat time before starting the pattern again.

The rest of the code is similar, but each section turns on the LEDs in a different pattern based on a binary sequence, with each LED representing a bit in the sequence. For example, the section that starts with digitalWrite(redLED,LOW) turns on the green LED to represent the binary number 0001. The binary numbers 0000 to 1111 are represented in this way, with each pattern being displayed for a duration of dat time before moving on to the next pattern.
Download the code here : ARDUINO CODE



Components for the projects:
1.Arduino uno
2. green led
3. yellow led
4.red led
5.blue led
6.resistor(330ohms)
7.battery
Circuit diagram of the binary led circuit

Comments

Popular posts from this blog

WHAT IS WEBSCRAPING IN PYTHON

  Web scraping is a technique that allows you to extract data from websites and store it in a format of your choice. It can be useful for various purposes, such as market research, price comparison, content analysis, and more. In this blog post, i will show you everything a beginner needs to know about web scraping, from the basics to some advanced tips and tricks.   What is web scraping?   Web scraping is the process of programmatically retrieving information from web pages. It involves sending requests to web servers, parsing the html code of the web pages, and extracting the data you want. Web scraping can be done manually, by copying and pasting data from a website, or automatically, by using a software tool or a programming language.   Why web scrape?   Web scraping can help you access data that is not available through an api or a downloadable file. For example, you may want to scrape product reviews from an e-commerce website, or news ...

Thevenin’s Theorem: A Beginner’s Guide

                                        table of content  Introduction History of Thevenin’s theorem Basic circuit analysis concepts Voltage, current, and resistance Ohm’s law Thevenin’s theorem: principles and applications Statement of the theorem Finding the Thevenin equivalent circuit What is Thevenin’s theorem? When should you use Thevenin’s theorem? How do you apply Thevenin’s theorem to a circuit Conclusion . Introduction Electrical engineers can effectively convert complicated circuits into smaller equivalent circuits by using Thevenin's theorem. The French telegraph engineer Léon Charles Thévenin is honored by having his theorem called in his honor. He proposed it in 1883. History of Thevenin’s theorem Hermann von Helmholtz, a German scientist, independently derived Thevenin's theorem in 1853; Léon Charles Thévenin did ...

AI Learning: How Machines Learn Like Humans

  Today I’m going to write about how the ways AI learns are similar to how I learn. Sounds interesting, right? let me clarify what I mean by AI. AI stands for artificial intelligence, which is a broad term that covers any system that can perform tasks that normally require human intelligence, such as understanding language, recognizing images, playing games, making decisions, etc. AI can be divided into four types: Strong AI: this is the ultimate form of artificial intelligence, it can reason on its own and act human like, it has not yet been made. Weak AI: it cannot truly reason and solve problems, but can act as if it were intelligent. Weak AI holds that suitably programmed machines can simulate human cognition. Applied AI: aims to produce commercially viable “smart” systems such as, for example, a security system that is able to recognise the faces of people who are permitted to enter a particular building. Applied AI has already enjoyed considerable success. Cognitive AI: compu...