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

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 ...

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 ...

Study Linear Regression: A Beginner’s Guide with c++

Table of content: • Introduction • What is Linear Regression? • Simple vs. Multiple Linear Regression • Implementing Linear Regression in C++ • Complex Linear Regression • Conclusion • References Understanding Linear Regression: A Beginner’s Guide Linear regression is a powerful statistical tool that allows us to understand the relationship between two or more variables. It is widely used in many fields, including finance, economics, and engineering, to make predictions and analyze data. In this article, we will explore the basics of linear regression, including what it is, how it works, and how to implement it in C++. We will also discuss the differences between simple and multiple linear regression and provide examples to help you understand these concepts. What is Linear Regression? At its core, linear regression is a method for finding the line of best fit that describes the relationship between two continuous variables. This line can be used to make predictions about o...