Arduino LED Projects with code (Best Tutorial 2019)

Arduino LED Projects code

Arduino LED Projects with Arduino LED code Tutorial 2019

In this tutorial, we have to explain the Arduino LED Projects with Arduino LED codeLED, we can start to control multiple LEDs with Arduino. We will start by making a simple traffic light module and then create a pair of LED glasses.

 

Simple traffic light controller

For making simple traffic light controller firstly we connect red, yellow, and green LEDs with pins 9, 10, and 11 respectively to Arduino LED. In the case of pin 13, it has an in-built pull-up resistor.

 

Pull up resistors are used to limit the current supplied to an LED. We can't give a current of more than a few mA to LEDs. But, with pin 13, current is itself limited in Arduino by the internal pull-up resistor.

Arduino LED Projects

 

 

In the preceding code, you can see that it is much the same as the "Hello World" program, except here we are controlling multiple LEDs. Here, we are initializing the variables as integers and using that same variable throughout the code.

 

So, in the future, if we need to change an Arduino pin, we just have to make a change in one place, instead of making changes at multiple places in the code. It is a good practice to use variables instead of directly using pin numbers in the code.

 

In the setup function, we are setting pins as OUTPUT. If we don't initialize the port to either INPUT or OUTPUT, a port might be in an indefinite state. So, it will give a random output.

We have completed the code for one direction of our traffic light. Similarly, you can create your code for the other remaining directions.

 

LED fading

Arduino LED

Using PWM on Arduino

Arduino UNO has 14 digital I/O pins. As mentioned in Blog Getting Started with Arduino and LEDs, we can use six pins (3, 5, 6, 9, 10, and 11) as PWM pins. These pins are controlled by on-chip timers, which toggle the pins automatically at a rate of about 490 Hz. As discussed earlier, we will be using the analogWrite() function.

 

In the following figure, the analogWrite() function takes the pin number and pin value as its parameter. Here, as you can see in the image, the pin value can be between 0 and 255, with the duty cycle mapped to 0% and 100%:

 

Connect the anode (longer head) of the LED to pin 11 (PWM pin) through a 220 ? resistor and the cathode (shorter head) to ground, and write the following code in your editor window:

int led = 11; // the pin that the LED is attached to

int brightness = 0; // how bright the LED is
int steps = 5; // how many points to fade the LED by
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
// Setting brightness of LED: analogWrite(led, brightness);
// change the brightness for next time through the loop: brightness = brightness + steps;
// When brightness value reaches either 0 or 255, reverse direction of fading
if (brightness == 0 || brightness == 255) { steps = -steps ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

 

We are using pin 11(PWM pin) for fading the LED. We are storing the brightness of the LED in variable brightness. Initially, we are setting 0 brightness to the LED. When the loop function runs again, we are incrementing the value by steps of 5.

 

As in the analogWrite() function, we can set the value between 0 and 255. Once brightness reaches a maximum, we are decrementing the value. Similarly, once brightness reaches 0, we start incrementing brightness in steps of 1. To see the dimming effect, we are putting a delay of 30 milliseconds at the end of the code.

 

LED Glasses

Glasses

In this blog, we will look at how to create a pair of LED glasses.

The materials needed are:

  • 30 LEDs (5 mm) in any color
  • A trinket board or an Arduino micro board
  • An FTDI to USB converter
  • Wires
  • A soldering iron
  • A 3-5V lithium battery (the smaller the better)
  • A JST female connector
  • 3 mm MDF
  • 220Ω resistors

 

Making the glasses

You can make your glasses out of anything you like and it might even be possible to modify a pair of existing sunglasses if they are big enough. In the figure, you will find the design of the glasses with measurements. Remember that these measurements might need to be modified so that the glasses it your particular head size.

 

The design of the glasses is very simple yet its the purpose. The design consists of three main pieces, the front, and two side frames. The two smaller pieces are used for added support when connecting the frames to the front panel.

 

The frames can be simply glued to the corners of the front panel, while the two small pieces are glued to the inside corners on each end.

 

The design template can be used to cut the pieces out from any material you like, but I recommend a sturdy material of at least 3 mm thickness. Plastic materials would also work for this project, or you could even make the frames out of cardboard, which can easily be cut by hand.

 

The glasses presented in this blog were cut from 3 mm MDF using a laser cutter. I don't expect most readers to have their own laser cutter, but many bigger cities today have something called hackerspaces or Fab Labs, and some even have them in their city's libraries.

 

The concept of Fab Labs was developed by the Massachusetts Institute of Technology, building on the idea of hackerspaces, which are locations that offer access to different fabrication tools and equipment where different people come to work on projects.

 

Hackerspaces are usually a place where people interested in electronics and programming meet to share ideas and work on projects. Fab Labs are more oriented toward fabrication, not a just digital entity, and are open to the public.

 

If you haven't checked already, I suggest you investigate whether there is a hackerspace or Fab Labs close by since you now have the perfect excuse to head over for a visit.

 

 If you choose to modify the design, remember to keep the size of the holes at 5 mm since this is the size of the LEDs we will be using. You can swap these LEDs for the smaller 3 mm ones if you like, but I would not recommend LEDs bigger than 5 mm since these might complicate the design.

 

Entering the matrix

LED_matrix

The current can only pass through an LED in one direction, a fact we are using to our benefit while creating the LED matrix, giving us the possibility of controlling many LEDs with fewer pins.

 

If we did not connect everything in a matrix, we would need 30 pins in order to control all the LEDs separately. The downside of using a matrix configuration is that we can only control one LED at a time.

 

However, we will take advantage of another phenomenon called POV (persistence of vision). POV is a term used to describe an optical illusion where multiple images blend into one image in the mind of the beholder. The brain can only interpret about 25 discreet images per second; any more than that and the images start to blend together.

 

Arduino is fast, even faster than the human eye, so we will use this speed to our advantage in order to give the impression of lighting up many LEDs at the same time.

 

As I said, we can't technically light up more than one LED at once in the matrix but we can switch between them so fast that the human eye will perceive it as more than one LED being on. But before we get to this part, we need to connect everything, and this means it is time to turn on the soldering iron.

 

Before we start soldering, we need to place the LEDs in the right order. A good idea is to check that all your LEDs work before soldering them. By connecting the LEDs one by one to a 3.3V coin cell battery, or using the power that goes from your Arduino to a breadboard, you can easily do this. If you are using a breadboard, don't forget to add a 220Ω resistor.

 

If you cut 5 mm holes, the LEDs should it nicely. If they are a bit lose don't worry, as once they are soldered together everything will be held in place. To create the matrix, we need to solder the LEDs into rows and columns.

 

I have prepared the LEDs for soldering by bending the legs of the LEDs into the desired rows and columns.

 

All the negative legs (the shorter ones) will be soldered in horizontal lines, and then the positive legs (the longer ones) will be soldered in vertical lines. Make sure that you bend the positive lines over the negative lines so they do not come into contact with one another.

 

If they do, the matrix will not work as it is supposed to. If you want, you can cover up your lines using some tape needed. This is done by placing a small piece of tape in between the legs of the LEDs so they do not touch one another.

 

Once you are done, we can move on and add the wiring and the resistors that will connect to our Arduino board. The figure shows a close-up of the resistors connected straight to the positive column in the glasses, and the wires connected to the other side of the resistors.

The idea is to place the Arduino board on the inside of either the left or right frame.

 

Before you cut your wires, measure the distance between the row and the location of the Arduino board where it will be placed on the inside of the side frame. Make sure you add some extra length before you cut them because it is better to have wires that are too long than too short.

 

Now is also a good time to put the JST female connectorJST connectors are a fairly standard connector for batteries, and in this project, we will be using a very small 3.7V battery with a male JST connector.

 

You can place the JST connector anywhere you like, but I found a good spot where the front panel meets the frame just under the supporting piece of MDF. Make sure you leave enough space on the back side of the connector to it the power cable that connects to the Arduino board. To keep the JST connector in place, use some glue:

 

When you have added the wires to all the positive columns, you can add three cables for the negative rows, again ensuring you make them long enough to reach the Arduino board. You don't need resistors on these lines since these will act as our GND channels.

 

Once you have all the LEDs, resistors, and wires in place, it is time to connect everything to the Arduino board. In this blog, we are using the Trinket board from Adafruit, but you could also use an Arduino micro board, which is very similar in size.

 

These boards are some of the smallest Arduino boards that offer most of the functionality of a standard Arduino board.

 

Soldering all the wires in place might be tricky. I started by gluing the board to the inside of the frame and then soldering the wires one by one. I would suggest that you place them where they it best. You can always switch the layout in the code later on. 

 

Take your time soldering all the wires in place. I admit that even someone with good soldering skills might find this project a bit tricky since it requires some unconventional soldering. I call this type of solder action "soldering" since you usually end up with something that looks like it came from a movie.

 

Usually, you solder components on a flat surface, but with wearable projects like this one you need to be a bit creative when it comes to soldering things together. Eventually, you will end up with an inside that is as impressive as the outside.

 

Next, we will move on to the programming sideand this is where we get to see the glasses in action. For the programming part, we will power the glasses via the USB cable, and once we are done we will add a battery, then you will be ready to head out into the night to impress everyone.

 

[Note: You can free download the complete Office 365 and Office 2019 com setup Guide for here]

 

Programming the glasses

Now let's make a sketch that checks that all of the LEDs light up. In order to do so, we will loop through the LEDs one by one to see that everything works as it is supposed to.

 

The Trinket is programmed as a normal Arduino. Uno board, so make sure you select this type in the board menu Upload the following code and check the LEDs in front of the glasses:

/*Collect all the positive columns pins in one array. You need to make sure that these pins correspond to the direction you have placed the columns in the glasses*/
int powerPin[]={
3,4,5,6,8,9,14,16,17,18,19};
/*Collect all the negative row pins in one array. Again make sure they are added in the same order corresponding to the glasses*/
int gndPins[]={ 10,11,12};
void setup(){
/*In order for the matrix to work we need to be able to control our gnd lines in the matrix. The trick is to use all pins as output. When we turn the gnd pins HIGH we will be able to block the ground connection*/
for(int i=0; i<20;i++){ pinMode(i,OUTPUT); } //Turn all the gnd pins HIGH in order to keep all LEDs off for(int j=0;j<3;j++){ digitalWrite(gndPins[j],HIGH); } } void loop(){ //Run the function looper(); } void looper(){ /*In this function we run through all the LEDs using two for loops starting by opening a gnd connection*/ for(int i=0; i<11;i++){ digitalWrite(powerPin[i],HIGH); //Once a gnd pin is accessible we turn on one of the LEDs for(int j=0;j<3;j++){ digitalWrite(gndPins[j],LOW); delay(50); digitalWrite(gndPins[j],HIGH); delay(50); } digitalWrite(powerPin[i],LOW); } //Loop everything backwards for(int i=10; i>=0;i--){
digitalWrite(powerPin[i],HIGH);
for(int j=3;j>=0;j--){
digitalWrite(gndPins[j],LOW);
delay(50);
digitalWrite(gndPins[j],HIGH);
delay(50);
}
digitalWrite(powerPin[i],LOW);
}
}

 

In this example sketch, we are implementing a trick in order to be able to control the ground connections. If we connected the negative rows of the matrix straight to GND we would not be able to control the separate LEDs.

 

The trick is to use normal pins as outputs. When a pin is LOW, it connects to ground, which we can use to light up our LEDs in the matrix.

 

But once it turns to HIGH, we block the connection to the ground. So now we can control each LED individually by turning one of the positive columns HIGH and one of the negatives rows LOW.

 

You will need to make sure that your pin declarations line up with the actual physical layout in your glasses or else looping through them could get very hard. As you can see in the schematic in Figure, the columns are connected after one another to the digital pins.

 

Finishing the glasses Knight Rider style

glass

To be more precise, the example is inspired by the robotic car featured in the show, which is called Kit. In the front of Kit, there is a small LED display that shows a bouncing light effect. This is the effect we will recreate on the front of the glasses.

 

The code example is fairly simple and does the same thing as the test sketch, but instead of lighting up only one LED at a time we will light up an entire column. We will move the light column from left to right and once we hit the end of the glasses, we will move the column back again:

int powerPin[]={ 19,18,17,16,14,9,8,6,5,4,3};
int gndPins[]={ 12,11,10};
int refreshRate=200;
void setup(){
for(int i=0; i<20;i++){ pinMode(i,OUTPUT); } // for(int j=0;j<3;j++) { digitalWrite(gndPins[j],HIGH); } } void loop(){ nightrider(); } // void nightrider() { /*Instead of starting to loop through the columns we loop through the row*/ for(int i=0; i<11; i++){ /*Then we loop through the column for(int j=0; j<3; j++){ /*In order to perceive that the column is lit we need to loop it a few times*/ for(int k=0; k<50; k++){ /*Then we light the column digitalWrite(powerPin[i],HIGH); digitalWrite(gndPins[j],LOW); delayMicroseconds(refreshRate); digitalWrite(powerPin[i],LOW); digitalWrite(gndPins[j],HIGH); } } } /*Once we have reached the end of the glasses we do the same thing backward*/ for(int i=11; i>0; i--){
for(int j=3; j>0; j--){
for(int k=0; k<50; k++){
digitalWrite(powerPin[i],HIGH);
digitalWrite(gndPins[j],LOW);
delayMicroseconds(refreshRate);
digitalWrite(powerPin[i],LOW);
digitalWrite(gndPins[j],HIGH);
}
} }

 

Once your code is done, upload it to the board and enjoy. In order to show off your new glasses, we need to finish up the circuit by attaching the battery to the glasses so you can walk around with them (since being forced to be connected to a USB port at all times might not look as cool).

 

Any battery between 3-5V will work for this project, and the more amperes the battery has, the longer the glasses will stay on. However, large ampere batteries are also bigger.

 

The smallest lithium battery I have found is the 3.7V 150mAh battery, which I recommend. There are smaller ones with fewer amperes but this type will still it your glasses and give you enough power to keep the glasses long enough for you to impress a few people.

 

Before you connect the battery, you need to solder wires from the JST connector to the board. The pins for external power on the Trinket board are marked BAT+ for the positive connection.

 

Next, to this pin, there is a pin marked with only a G for the ground connection. This connection is connected to the negative side of the JST connector.

 

In order to figure out which pin is which on the JST connector, check with the battery cables. Usually, these cables are red and black; the red one is the positive and the black one is the ground.

 

The design of the glasses should leave enough space on the inside of the frames to place a small battery.

 

I recommend gluing the battery in place using hot glue, but be careful not to heat the battery for too long or it might explode. This might sound scary, but you would need to heat it for some time before it explodes. However, I always recommend caution when combining heat and batteries.

 

As you might have noticed by now, visibility is limited in these glasses and it has to be fairly dark for the light to be visible, so be careful when using them. You can still look cool while keeping safe.

 

Project 2 – Remote Controlled TV Backlight

What is IR LED?

An IR (infrared) LED, also known as an IR (infrared) transmitter, transmits infrared waves in the range of 875 nm to 950 nm. Usually, IR LEDs are made up of gallium arsenide or aluminum gallium arsenide.

 

The working principle of an IR LED is the same as we mentioned in the previous blogs. The longer lead of the LED is the anode and the shorter one is the cathode, as shown here:

 

Applications of IR LED / IR communication

Apart from using IR communication in TV remote controls, there are a number of other applications that use IR communication. Infrared light can also be used to transfer data between electronic devices.

 

Although infrared light is invisible to the eye, digital cameras and other sensors can see this light, so infrared light can be used for many security systems and night vision technology.

 

Apart from technical uses, the U.S. Food and Drug Administration Department has approved several products with IR LEDs for use in some medical and cosmetic procedures.

 

IR sensors

IR_sensor

 

Programming a basic IR sensor

After understanding the basic workings of a simple IR sensor, we will learn how to program this sensor. Based on the principle of direct incidence, we will develop a simple burglar alarm. Once you have connected the circuit as shown in the following diagram, upload the following code to Arduino:

int IRTransmitter = 8;
int IRReceiver = A0; //IR Receiver is connected to pin 5
int buzzer = 9; // Buzzer is connected with pin 9.
int output = 0; // Variable to store the value from the IR sensor int ambientLight = 500;
void setup()
{
pinMode(IRReceiver,INPUT);
pinMode(IRTransmitter,OUTPUT);
pinMode(buzzer,OUTPUT);
digitalWrite(IRTransmitter,HIGH);
}
void loop()
{
output = analogRead(IRReceiver);
// If anything comes in between the IR Transmitter and IR receiver
// IR receiver will not give the output. Make an alarm.
if (output < ambientLight)
{
makeAlarm(50);
} }
void makeAlarm(unsigned char time)
{
analogWrite(buzzer,170);
delay(time);
analogWrite(buzzer,0);
delay(time);
}

 

In the preceding code, we are continuously making the IR transmitter ON. So, the IR receiver/photodiode will continuously receive an IR signal. Whenever any person tries to break into the house or safe, the IR signal will get interrupted, which in turn will lower the voltage drop across the IR receiver and an alarm will be raised.

 

Here, based on the conditions around you, you will have to change the value of the ambient light variable in the code.

 

How to receive data from a TV remote

Remote

As we discussed earlier, a TV remote has an IR LED, which transmits IR light with a 38 kHz frequency. So, whenever that radiation is detected at the receiver part of the TV (the control circuit of the TV), we need to filter that signal.

As you can see in the following image when a signal is actually transmitted from the TV remote, it will look like a series of waves:

 

For filtering the actual signal from a TV remote from ambient noise, we will use TSOP38238 IC. TSOP38238 looks like a transistor, but actually, this device combines an IR sensitive photocell, a 38 kHz bandpass filter, and an automatic gain controller.

 

Here, the IR sensitive photocell works as an IR receiver (photodiode), and the 38 kHz bandpass filter is required to smooth the received modulated signal.

 

After passing through the 38 kHz bandpass filter, the output signal will look as shown in the following, which is much cleaner and easier to read:

 

Also, TSOP38238 is covered in the lead package epoxy frame, which acts as an IR filter. So, the disturbance light (DC light from a tungsten bulb, or a modulated signal from fluorescent lamps) does not reach the photodiode.

 

This demodulated signal can be directly decoded by any microprocessor (in our case, Arduino). Because of this, apart from the preamplifier, it ignores all other IR light unless it is modulated at a specific frequency (38 kHz).

 

This IC is as simple as an IC can get with its three pins. There are two power pins and one pin for the output signal:

TSOP38238 can be powered with a supply from 2.5 V to 5.5 V, which makes it suitable for all types of application. Also, it can receive signals from almost all types of remotes.

 

To start with, we will control a single LED using an IR remote. For that, we will be using the IRRemote library, available at https://github.com/z3t0/Arduino-IRremote.

 

Once you have downloaded and extracted the code in the libraries folder of Arduino, you will be able to see the examples in the Arduino IDE. We need to record the value of all the buttons for future use.

 

Connect the first pin of TSOP38238 to pin 11 of Arduino, the second pin to the ground pin of Arduino, and the third pin to the 5 V pin of Arduino. Connect Arduino over USB and try to compile the following code:

#include <IRremote.h>
const int IR_RECEIVER = 11;
IRrecv receiver(IR_RECEIVER);
decode_results buttonPressed;
void setup()
{
Serial.begin(9600);
receiver.enableIRIn(); // Start the receiver
}
void loop()
{
if (receiver.decode(&buttonPressed))
{
Serial.println(buttonPressed.value); //Printing values coming from the IR Remote
receiver.resume(); // Receive the next value
}
delay(100);
}

 

Update the code for controlling the LED, based on your readings from the previous exercise:

 

#include <IRremote.h>
const int IR_RECEIVER = 11;
IRrecv receiver(IR_RECEIVER);
decode_results buttonPressed;
long int buttonValue = 0;
const long int buttonOne = 54528; //Update value one according to your readings
const long int buttonTwo = 54572; //Update value two according to your readings
int LEDpin = 9;
void setup()
{
Serial.begin(9600);
receiver.enableIRIn(); // Start the receiver pinMode(LEDpin,OUTPUT);
}
void loop()
{
if (receiver.decode(&buttonPressed))
{
buttonValue = buttonPressed.value;
Serial.println(buttonValue);
switch (buttonValue){
case buttonOne:
digitalWrite(LEDpin,HIGH);
break;
case buttonTwo:
digitalWrite(LEDpin,LOW);
break;
default:
Serial.println("Waiting for input. ");
}
receiver.resume(); // Receive the next value
}
delay(100);
}

 

Now you will be able to control a single LED using the IR remote. In a similar way, you can control anything that you want to control using an IR remote. At the end of this blog, we want to control a TV backlight using an IR remote.

 

Tweet-a-Light – Twitter-Enabled Electric Light

light

In this blog, we will learn:

  • How to install Python on Windows
  • How to install some useful libraries on Python, including pySerial and Tweepy
  • How to create a Twitter account and obtain Twitter API keys
  • How to write a simple Python Script to read Twitter tweets and write data on the serial port
  • How to write a simple Arduino sketch to read incoming data from the serial port

 

Hardware and software requirements

To complete this project, you will require the following hardware and software.

Hardware

Arduino UNO Rev3 board (https://store.arduino.cc/product/A000066)

A computer with Windows installed and Internet-connected

PowerSwitch Tail (120V or 240V depending on your voltage of mains electricity supply)—(http://www.powerswitchtail.com/Pages/default. aspx)

A light bulb (120V or 240V depending on your voltage of mains electricity supply), holder, and wires rating with 120V/240V

A USB A-to-B cable (https://www.sparkfun.com/products/512)

Some hook-up wires

 

Software

The software needed for this project is mentioned under each topic so that it will be easier to download and organize without messing things up.

 

Installing Python on Windows

The following steps will explain how to install Python on a Windows computer:

1. Visit https://www.python.org/.

2. Click on Downloads | Windows. The Python home page

3. Then, you will navigate to the Python Releases for Windows web page:

 

4. Python can be downloaded from two development branches: legacy and present. The legacy releases are labeled as 2.x.x, and present releases are labeled as 3.x.x.

 

5. Click on the latest (see the date) Windows x86-64-executable installer to download the executable installer setup file to your local drive under Python 3.x.x.

 

6. Alternately, you can download a web-based installer or embedded ZIP file to install Python on your computer.

 

7. Browse the default Downloads folder in your computer and find the downloaded setup file. (My default downloads folder is C:\Downloads). Double-click on the executable file to start the setup:

 

8. Click on the Run button if prompted as a security warning:

 

9. The Python setup wizard starts:

Optionally, you can check Add Python 3.5 to PATH, or later, you can add it using Windows system properties. Click on the Customize installation section. The Optional Features dialog box will appear:

 

10. Click on the Next button to proceed. The Advanced Options dialog box will appear. Keep the selected options as default.

 

The default installation path can be found under Customize install location. If you like, you can change the installation location by clicking on the Browse button and selecting a new location on your computer's local drive.

 

11. Finally, click on the Install button.

If prompted for User Access Control, click on OK. The Setup Progress screen will be displayed on the wizard: Python setup installation progress

 

12. If the setup is successful. Click on the Close button to close the dialog box:

 

Python Command Prompt

This ensures that the Python environment variables are successfully added to Windows. From now, you can execute Python commands from the Windows command prompt. Press Ctrl + C to return the default (Windows) command prompt.

 

Installing the setup tools utility on Python

The setup tools utility lets you download, build, install, upgrade, and uninstall Python packages easily. To add the setuptools utility to your Python environment, follow the next steps. At the time of writing this blog, the setuptools utility was in version 18.0.1.

 

Visit the setuptools download page at https://pypi.python.org/pypi/ setuptools.
Download the ez_setup.py script by clicking on the link (https:// bootstrap.pypa.io/ez_setup.py):

The script opens in the browser's window itself, rather than downloading as a file. Therefore, press Ctrl + A to select all the code and paste it into a new Notepad file:

 

4. Next, save the file as ez_setup.py in your local drive.

 

5. Open Windows Command Prompt and navigate to the location of the ez_setup.py file using the cd command. We assume that the drive is labeled as the letter D:, and the folder name is ez_setup: C:\>D:

D:\>CD ez_setup

 

6. Type python ez_setup.py and press the Enter key to run the Python script. This installs the easy setup utility package on your Python environment:

 

Recommend