Pulse Width Modulation using 555 Timer in Proteus

Hello Engineers! Welcome to the board. We hope you are having a good day. In this tutorial, we teach you about Pulse Width Modulation. We'll discuss some important points about the topic. Let's have a look at the Topics of the tutorial:
  1. What is Pulse Width Modulation?
  2. What is 555 Timer?
  3. how does 555 Timer is used in the Pulse Width modulation circuit?
  4. How do we design the circuit of Pulse Width Modulation in Proteus ISIS?
In addition, you will have some useful information bout Pulse Width Modulator in DID YOU KNOW section.
Where To Buy?
No.ComponentsDistributorLink To Buy
1555 TimerAmazonBuy Now
2LEDsAmazonBuy Now
3ResistorAmazonBuy Now

Pulse Width Modulation

Pulse width Modulation is a useful technique in the world of Modern Electronics. Let's have a look at the information about Pulse Width Modulation.

Abbreviation of Pulse Width Modulation

The Abbreviation of the Pulse Width Modulation technique is PWM.

Definition of Pulse Width Modulation

We define the pulse Width Modulation as:
"The Pulse Width Modulation is the technique  in the electronics to control the power given to the analogue devices through which the average power delivered by the electrical signal is reduced due to division of the signals into discrete parts."
The Pulse Width Modulation is important to the inertial load devices such as motors because in these devices the change is slow due to their inertial ability and the Pulse Width Modulator has enough time to control the device.

Example of PWM

We know that in the bulb that we use in our daily life, the AC Power changes its direction from positive to negative cycle and vise versa. The frequency through which the cycle change decides the brightness of the bulb. Consider the example of the circuit in which the LED is connected to the power. The Power connection lightens the LED. When the switch between the power source and the bulb is close, the power is transmitted to the bulb and the brightness is observed. The opening and the closing of the switch can be controlled through the Pulse Width Modulation. The more is the duty cycle of the Pulse width, the more rapidly it opens and closes the switch and hence the brightness of the bulb is more and vise versa. hence we can conclude that by controlling the pulse width we are controlling the opening and closing of the switch and through which we are controlling the brightness of the bulb.  

DID YOU KNOW????????????

"The Pulse Width Modulation technique is also called as the Pulse Duration Modulation or PDM. It is because this technique works with the duration of the cycle of the circuit. "

555 Timer in Pulse Width Modulation

Prior to start the work of 555 Timer in Pulse Width Modulator, we must clear some important concepts about the 555 Timer device. Let's have a glance on the points.

Definition of 555 Timer

The 555 Timer was termed as the SE NE555 Timers. Another Type of the 555 Timer is SE555 Timer. These were first invented by "Signetic Corporation" . We define the 555 Timer as:
"The 555 Timer is an 8 pin Integrated Circuit that generates accurate timing pulse. The designing of the 555 timer is done by collectively arranging the electrical and electronic components such as resistors, transistors, diodes and Flip Flops."
These are monolithic Timing circuits that are designed to provide stable time delay and oscillations. These are highly reliable and low in cost.

Pin out of 555 Timer

There are 8 pins of 555 Timer and each pin has its own function and operation. For the best concept, we have designed a table for each pin given below:
Pin Number Pin Name Description
1 Ground This pin is labeled as GND and used to supply the 0 voltage.
2 Trigger When the Time interval starts, the output remains low when this pin is high and vise versa.
3 Output This is the output pin.
4 Reset This pin overrides the Trigger pin and that overrides the Threshold. It is connected with Vcc if not used.
5 Control It controls the Pulse Width and the level of threshold and Trigger.
6 Threshold Hen the voltage is applied at this pin, it acts in the contrast to the voltage.
7 Discharge This pin is an open-collector output. During the intervals, this pin is used to discharge the capacitor.
8 Supply This is the power supply pin. The input of power is taken against the Ground pin.

DID YOU KNWO???????????

"We use the counter instrument in the Proteus to count the Pulse Width Modulation of the circuit using 555 Timer."

Implementation of PWM using 555 Timer in Proteus ISIS

To implement the Pulse Width Modulation using 555 Timer, we are using the simulation in Proteus ISIS. To Implement the 555 Timer PWM  just follow the simple steps given next:
  • Fire up your Proteus Software.
  • Choose the following components:
  1. 555 Timer
  2. 1N4148 Diode
  3. 3005P-1-502 Variable Resistor
  4. DC Power Supply
  5. Resistor
  6. Counter
  7. Oscilloscope
  • Fix first five components from the "Pick Library" at the working area.
  • Change the values of Resistor, Capacitors and variable Resistors according to the table given below:
    Component Values
    Resistor 1k ohm
    Variable Resistor 50k ohm
    Capacitor 1 10nf
    Capacitor? 1uF
    DC Power source 10V
     
  • Go to Terminal mode>Ground and set it at the end of the circuit.
  • Connect the components through the connecting wires according to the image given next:
  • To get the counting of the output, go to virtual Instrument Mode and choose the counter.
  • Go to Virtual instrument Mode and select the Oscilloscope.
  • Connect the counter with any terminal of the Oscilloscope.
  • Join both the instruments with pin 3 of 555 Timer.
  • Pop the play button and simulate the circuit.
  • Change the values of voltages and the frequency according to need.
  • We observe that the Oscilloscope shows us the width as:
Thus, Today we saw what is the pule Width Modulation, learned some important concepts about the 555 Timer, got some important concepts about Pulse Width Modulation using the 555 Timer and saw the simulation by the mean of Proteus ISIS. If you found it useful, give us your important feedback in the comment section.

Metal Detector using 555 Timer in Proteus

Hello Pupils! I welcome you to the board. I hope you are fine. In today's tutorial, we will design a project Metal Detector using 555 Timer in Proteus ISIS. All of us perceive the situations when at the public places such as on airports or in shopping malls where sharp metallic objects such as a knife or illegal guns or even a nail cutter are not allowed, there are walkthrough gates at every entrance so that any person with the forbidden material when passes through the gate, the alarming buzzer automatically switched on. This happened because the walkthrough gates have the Metal Detector circuit in them that works immediately when such a situation occurs. In this session, we'll learn:
  1. What are Metal Detectors?
  2. How does the 555 Timer collaborate with the circuit of Metal Detector?
  3. How does the circuit of the 555 Timer Metal Detector works?
  4. How can we implement the circuit of 555 Timer Metal Detector in Proteus?
In addition, you will also have some useful pieces of information in DID YOU KNOW Sections.
Where To Buy?
No.ComponentsDistributorLink To Buy
1555 TimerAmazonBuy Now
2LEDsAmazonBuy Now
3ResistorAmazonBuy Now

NE555 Timer Metal Detectors

Metal Detectors became the one of the necessary devices for many public Places either it is park or bank, airport or any supermarket. It is because they play a vital role in the maintenance of security. Most of the metal detectors We define the Metal Detector as:
"The Metal detectors are the specialized NE555 Timer gadgets that detect the presence of the metals when the metals enters in the range of Metal Detector Circuits."
The NE555 Timer Metal Detectors does not only have the application i the field of security but they are also used in a variety of situations. The NE555 Timer Metal Detector can be categorized into three basic Families:
  1. Very Low Frequency Detectors.
  2. Pulse Induction Detectors.
  3. Metal Detectors for specific Purpose.

DID YOU KNOW??????????????????????

"1960 was the year in which the 1st Metal Detector was established using simple devices in industrial and Mineral Prospecting. "
There are many kinds of NE555 Timer Metal Detectors that are used for different operations some of them are listed below:
  1. Diagnostic Purposes.
  2. Detecting myriad of foreign objects.
  3. Finding the presence of bullets.
  4. Detecting the intraocular metallic fragments.
  5. Finding Swallowed coins.

Working of NE555 Timer Metal Detector

When we talk about the working of the NE555 Timer Metal Detector, we must have a clear idea about the following concepts:
  1. Inductance
  2. NE555 Timer operational system

Inductance in NE555 Timer Metal Detector

Let's recall the idea of Inductance that we are learning from our physics class:
"The Inductance, in electromagnetism and electronics, is the ability of a conductor of electricity to negate the change in the electric current that is flowing through it. This flow of electric current produces a magnetic field around that electrical conductor."
In the NE555 Timer Metal Detector circuit, we use an inductor that senses the presence of the Metal near to it. More close a Metal Detector to it, the more electric field lines are produced and hence the speaker gives the sound more loudly indicating the distance between the NE555 Timer Metal Detector and the metal's distance. We denote the Inductance through "L". Hence the formula to find the Inductance through any Conductor can be find through: Inductance= Magnetic Flux of Current/Current.

DID YOU KNOW ???

"There are some Metal Detectors that are used to find the treasure or ancient metals underground. They are so powerful that they can detect the Metal many feet away.  Thus, many people have a life changing search due to these useful instruments."

NE555 Timer

NE555 Timers belongs to the Family of 555 Timers Integrated Circuits. These are highly utilitarian circuits that are considered as one of the most used Integrated Circuits in the world of Electronics. We introduce the NE555 Timer as:
"NE555 Timer Circuit is the widely used Integrated Circuit having 8 pins and used to have the output that have a uniform pulses that can be set according to need."
The 555 Timers are used to have a variety of pulses that depends upon the arrangement of the devices connected to their Pins. There are three kinds of NE555 Timer modes:
  1. Monostable Mode.
  2. Astable Mode.
  3. Bistable Mode.

Operations in the NE555 Timer Metal Detector Circuit

  • When we examine the Circuit of NE555 Timer Metal Detector, we find these operations:
  • The power of all the components is the Direct current that is provided by the battery.
  • This power enters the NE555 Timer circuit that produces the uniform Timer-based Pulse at its output pin.
  • This Pulse enters the resistor that controls the flow of current through the main Metal Detector circuit.
  • The Resistor passes this current to the Inductor. The Inductance of the inductor is the basic criteria of distance measurement.
  • As in Proteus, it is not possible to show the Movement of a Metal, so the value of the inductance represents the number of electric field lines around the NE555 Timer Metal Detector circuit. More is the Inductance, more numbers of lines passing through the inductor and hence it is assumed that metal is more near to the circuit. 
  • The DC current then passes through the speaker according to the strength of the electric field lines and hence we found the faint or hard sound.
[PostWiidget4]

Circuit design of 555 Timer Metal Detector in Proteus

  • Power up your Proteus Software.
  • Choose the following components from the Pick Library button "P".

Components Required

  1. NE555 Timer
  2. Inductor
  3. Capacitor
  4. Resistor
  5. Speaker
  • Take all the Components from the left section and arrange all of them on working screen according to the diagram given below:
 
  • Now, Change the values of some of the components one after the other by double clicking the components.
  • Inductor= 150mH, Capacitor 1=2.2uF,Capacitor 2=2.2uF,Capacitor 3 10uF, Resistor= 47k Ohm, Battery=9V.
  •  Connect the circuit components with the help of connecting wire so our circuit look like this:
  • As soon as the Circuit is simulated by hitting the Play button, the user sense a sound or buzzer from the circuit.
  • If you heard it then cool, otherwise look at your circuit once again.

Task

Change the value of inductor to 300micro Farad and more to hear the louder sound.
Hence today, we saw what are NE555 Timer Metal Detector, How do they are classified, how does the circuit of 555 Metal detector works and how can we design its circuit using simple devices in Proteus ISIS. Stay with us for more projects.

Police Siren Project using 555 Timer in Proteus

Hey Geeks! Welcome to The Engineering Projects. We hope you are having a reproductive day. We know that sirens are the special sounds that are the symbol that something unusual is occurring or about to occur. You may have experienced the Siren of the Walkthrough Gates at the airport when a person having the knife or other forbidden material pass through it. Or you have heard the Siren of the ambulance and seen that all the traffic gives the way to the ambulance when they hear the special Siren of the Ambulance. The same is the case with the police Siren. The Police sirens are the special sound and it is set with the help of 555 Timer Integrated Circuit. You will learn how can one design a Police siren using the 555 Timer circuit in this tutorial. Let's have a quick list of the topics that will be clear in our tutorial.
  1. What is the 555 Timer Police Siren?
  2. What are the 555 Timer and its modes?
  3. How does the circuit of 555 Timer police Timer Circuit works?
  4. How can you design the circuit of 555 Timer Police Siren in Proteus?
Where To Buy?
No.ComponentsDistributorLink To Buy
1555 TimerAmazonBuy Now
2LEDsAmazonBuy Now
3ResistorAmazonBuy Now

555 Timer Police Siren

The Police Siren we have seen many times in real life as well as in Television shows and Movies are made of the special arrangement of the 555 Timer. The Siren has a loud voice that can be heard at a distance of many feet. This Project has a very simple yet amazing arrangement of some basic electronic devices. The heart of Police Siren is the 555 Timer integrated circuit. In the police siren, two 555 timers are used. This is a Multi-functional chip that is widely used in different types of the industrial as well as household applications. If we look at the configuration of 555 Timer Integrated Circuit then we can generate a table just as shown next:
Pin Number Attachments
1 Ground
2 Trigger
3 Output
4 Reset
5 Control
6 Threshold
7 Discharge
8 Vcc
Technically, The 555 Timer works in 3 modes:
  • Monostable Mode
  • Astable Mode
  • Multistable Mode

Monostable Multivibrator Mode in 555 Timer

This mode of the 555 Timer contains a single stable state that can be used to get only one single output pulse of a specific width that may be high or low by applying an external trigger pulse. In this circuit, the 555 Timer uses only one resistor but two capacitors.

Astable Mode in 555 Timer

As the name shows, the Astable mode does not have any stable state. The Astable mode of 555 Timer has 2 quasi-steady states that change from one state to another one after the other. In this way, the 555 Timer in this state, alters the output from high to low and vise versa after the time settled by the user. It uses two capacitors and two resistors connected with the specific pins in a specific manner.

Bistable Mode of 555 Timer

In this mode of 555 Timer, the pins are connected with two resistors, one capacitor and two switches. The switches turn the state of 555 Timer to high and low and thus we obtained the high and low output waves at a time.

Working of the 555 Timer Police Siren

The working of the 555 Timer Police Siren starts from the Direct Current power supply that is supplied to pins 8 of the 555 Timer.
  1. Both of these 555 Timers are in the Astable mode that means their pulse output changes continuously.
  2. The 555 Timer at the left produces a pulse that is fed into the right 555 Timer as an input.
  3. The values of Resistors control the width of the pulses.
  4. The capacitors connected with the 555 Timers charge and discharge without any interval.
  5.  At the end, this DC power supply enters the speaker with a continuously varying pulses of the current and produces a special sound.
  6. If you want to change the output voice, you can change the values of Resistors and capacitors.

Circuit design of 555 Timer Police Siren in Proteus

To design this circuit, simply follow these step given next as it is.
  • Start the Proteus Software.
  • Choose the required devices from the pick library by clicking the "P" button and writing the names of the devices.

Required Components fpr 555 Timer Police Siren:

  1. NE555 Timer (We'll use 2 ICs)
  2. Diode
  3. Resistor
  4. Direct current power supply
  5. Speaker
  6. Capacitor
  • Get the 555 Timer from the library and arrange it at the working area.
  • Repeat the step above.
  • Choose Resistor, capacitor, Diode and speaker and arrange them on the screen.
  • Change the alignment of 4 resistors and diode by left click on screen> Rotate Clockwise and set the whole circuit as shown in the figure:
  • Go to Generation Mode>DC and fix it at above the working area.

DID YOU KNOW ???

"If you have the Proteus 8 software, then you can have a real time Siren sound by choosing the Speaker and a piano symbol with it."
  • Label the Components by double-clicking it because Proteus throws an error for the duplicate devices.
  • Double click the components mentioned below and change their values according to the table given next:
Device Value
R2 68k Ohm
R3 68K Ohm
R4 8.2K Ohm
R5 8.2K Ohm
C1 100uF
C2 100nF
C3 0.01uF
C4 10uF
Vcc 4V
 
    • Go to Terminal Mode>Ground and Set the Ground terminal just below the circuit.
  •  Join the 555 Timer's pins with the components as described above in the 555 Timer section.
  • Pop the Play button and simulate the circuit.

Task

Now, change the values of capacitor and resistor in different sequence to have the different voices as an output.
Have you heard the siren? If yes then cool. Yet, if no, then look at the circuit deeply and fix the mistake. Truss, today we saw that what is the Police Siren, how does the 555 Timer circuit works, what is the working mechanism of the 555 Timer Police Siren, how does we design the circuit of 555 Timer Police Siren in the Proteus. If you found it useful, give us feedback. If you faced any difficulty, share with us i the comment section. Stay with us with more Proteus Projects.

Interfacing of Arduino with 74HC595 & 74HC165

Hello friends, I hope you all are doing great. In today's tutorial, I am going to show you How to Interface Arduino with 74HC595 & 74HC165. I have already interfaced these shift registers separately with Arduino. In the first tutorial we have seen Arduino 74HC595 Interfacing in which I have discussed How to increase the output pins of Arduino using 74HC595. After that in second tutorial we have seen Arduino 74HC165 Interfacing where we have increased the input pins of Arduino. So, now we are gonna interface both of these shift registers with Arduino UNO and will increase both input and output pins of Arduino. I have also given the Proteus simulations for download at the end of this tutorial along with Arduino code. So, lets get started with Interfacing of Arduino with 74HC595 & 74HC165:

Interfacing of Arduino with 74HC595 & 74HC165

  • As you can see in above figure, I have used 74HC165 & 74HC595 and interfaced its pins with Arduino UNO.
  • I could use same clock for these shift registers but it would have made the code quite complex.
  • That's why I have used separate clock pins and I have used the below code to reflect the input on output.
#define NUMBER_OF_SHIFT_CHIPS   1
#define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8
#define TotalIC 2
#define TotalICPins TotalIC * 8

int LoadPin    = 8;
int EnablePin  = 9;
int DataPin    = 11;
int ClockPin   = 12;

int RCLK = 5;
int SER = 6;
int SRCLK = 7;

unsigned long pinValues;
unsigned long oldPinValues;
boolean Data[TotalICPins];

void setup()
{
    Serial.begin(9600);

    pinMode(LoadPin, OUTPUT);
    pinMode(EnablePin, OUTPUT);
    pinMode(ClockPin, OUTPUT);
    pinMode(DataPin, INPUT);

    digitalWrite(ClockPin, LOW);
    digitalWrite(LoadPin, HIGH);
    Serial.println("Visit us at www.TheEngineeringProjects.com");
    Serial.println();
    pinMode(SER, OUTPUT);
    pinMode(RCLK, OUTPUT);
    pinMode(SRCLK, OUTPUT);

    ClearBuffer();
    
    pinValues = read_shift_regs();
    print_byte();
    oldPinValues = pinValues;
}

void loop()
{
    pinValues = read_shift_regs();

    if(pinValues != oldPinValues)
    {
        print_byte();
        oldPinValues = pinValues;
    }

}

unsigned long read_shift_regs()
{
    long bitVal;
    unsigned long bytesVal = 0;

    digitalWrite(EnablePin, HIGH);
    digitalWrite(LoadPin, LOW);
    delayMicroseconds(5);
    digitalWrite(LoadPin, HIGH);
    digitalWrite(EnablePin, LOW);

    for(int i = 0; i < DATA_WIDTH; i++)
    {
        bitVal = digitalRead(DataPin);
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

        digitalWrite(ClockPin, HIGH);
        delayMicroseconds(5);
        digitalWrite(ClockPin, LOW);
    }

    return(bytesVal);
}

void print_byte() { 
  byte i; 

  Serial.println("*Shift Register Values:*\r\n");

  for(byte i=0; i<=DATA_WIDTH-1; i++) 
  { 
    Serial.print("P");
    Serial.print(i+1);
    Serial.print(" "); 
  }
  Serial.println();
  for(byte i=0; i<=DATA_WIDTH-1; i++) 
  { 
    
    Serial.print(pinValues >> i & 1, BIN); 
    Data[i] = pinValues >> i & 1, BIN;
    //if(BinaryValue == 1){Data[i] = HIGH;}
    //if(BinaryValue == 0){Data[i] = LOW;}
    UpdateData();
    if(i>8){Serial.print(" ");}
    Serial.print("  "); 
    
  } 
  
  Serial.print("\n"); 
  Serial.println();Serial.println();

}

void ClearBuffer()
{
    for(int i = TotalICPins - 1; i >=  0; i--)
    {
       Data[i] = LOW;
    }
    UpdateData();
} 

void UpdateData()
{
   digitalWrite(RCLK, LOW);
   for(int i = TotalICPins - 1; i >=  0; i--)
   {
        digitalWrite(SRCLK, LOW);   
        digitalWrite(SER, Data[i]);
        digitalWrite(SRCLK, HIGH);

  }
  digitalWrite(RCLK, HIGH);
}
  • In the above code, I have used Number_of_Shift_Chips 1 and it means I am using 1 chip each, so in total 2 chips.
  • Now get hex file from Arduino software and upload it in your Proteus software.
  • Run your simulation and if everything goes fine then you will get something as shown in below figure:
  • You can see in above figure that all those LED outputs are ON which has HIGH inputs.
  • I have also attached a Virtual Terminal with Arduino to have a look at the input bits.
  • Now let's add 2 chips of 74HC165 and 74HC959, so design a simple simulation as shown in below figure:
  • Now in your above code change the Number of Shift chips from 1 to 2, as now we are using 2 chips each.
  • Upload your hex file and if everything goes fine then you will get similar results:
  • So, that's how you can easily increase input and output pins of Arduino UNO.
  • I have just designed a simple code but you can work on it and can control these inputs separately as well.
  • You can interface different digital sensors on these input pins and can control motors, relays, solenoids etc. at output pins.
  • You can download both of these Proteus Simulations along with Arduino code by clicking the below button, but I would suggest you to dwsign it on yoru own so that you could learn from mistakes.

[dt_default_button link="https://theengineeringprojects.com/ArduinoProjects/Interfacing%20of%20Arduino%20with%2074HC595%20&%2074HC165.zip" button_alignment="default" animation="fadeIn" size="medium" default_btn_bg_color="" bg_hover_color="" text_color="" text_hover_color="" icon="fa fa-chevron-circle-right" icon_align="left"]Download Proteus Simulation & Arduino Code [/dt_default_button]

So, that was all about Interfacing of Arduino with 74HC595 & 74HC165. I hope you can now easily simulate it. If you have any questions then ask in comments and I will try my best to resolve them. Thanks for reading. Take care !!! :)

Arduino 74HC595 Interfacing: Increase Output Pins

Hello friends, I hope you all are doing great. In today's tutorial, I am going to show you Arduino 74HC595 Interfacing and we will have a loook at How to Increase Arduino Output Pins with 74HC595. Suppose you are working on some project where you need to control 20 LEDs with Arduino UNO and you know we will 12 digital Pins so we can't control all of these 20 LEDs with Arduino UNO. We can use Arduino Mega as well but if we wanna stick to Arduino UNO then we need to increase its Output Pins and we will use 74HC595 for that purpose. You should read this basic Introduction to 74HC595, it will help you to better understand this shift register. It's a Serial In Parallel Out Shift register and we will give it value serially from single Pin of Arduino and it will output that data to 8 output pins. Moreover, we can also connect these registers in parallel to increase the output pins even further. So, let's have a look at Arduino 74HC595 Interfacing:

Arduino 74HC595 Interfacing

  • As I told earlier 74HC595 is a serial In Parallel Out Shift Register and is used to increase the output pins of Arduino.
  • I am gonna use Proteus software and we will design its simulation and then will check out How it works.
  • So, design a simple circuit as shown in below figure:
  • As you can see in above figure, I have done the following connections between Arduino and HC595:
    • Pin # 5 of Arduino ==> ST_CP
    • Pin # 6 of Arduino ==> DS
    • Pin # 7 of Arduino ==> SH_CP
    • All output pins of 74HC595 are connected to LEDs.
  • Now upload the below Arduino code and get your hex file.
int RCLK = 5;
int SER = 6;
int SRCLK = 7;

#define TotalIC 1
#define TotalICPins TotalIC * 8

boolean Data[TotalICPins];

void setup()
{
  pinMode(SER, OUTPUT);
  pinMode(RCLK, OUTPUT);
  pinMode(SRCLK, OUTPUT);

  ClearBuffer();
}              


void loop()
{
   for(int i = TotalICPins - 1; i >=  0; i--)
   {
      Data[i] = HIGH;
      UpdateData();
      delay(300);
      ClearBuffer();
   }

   for(int i = 1;i < TotalICPins - 1;  i++)
   {
      Data[i] = HIGH;
      UpdateData();
      delay(300);
      ClearBuffer();
   }
   
}

void ClearBuffer()
{
    for(int i = TotalICPins - 1; i >=  0; i--)
    {
       Data[i] = LOW;
    }
    UpdateData();
} 

void UpdateData()
{
   digitalWrite(RCLK, LOW);
   for(int i = TotalICPins - 1; i >=  0; i--)
   {
        digitalWrite(SRCLK, LOW);   
        digitalWrite(SER, Data[i]);
        digitalWrite(SRCLK, HIGH);

  }
  digitalWrite(RCLK, HIGH);
}
  • The code is quite simple but let me explain it a bit.
  • First of all we have given names to our 3 Pins connected to Arduino UNO.
  • After that we have made all those 3 Pins as OUTPUT as we are gonna send the data.
  • We are using single chip of 74HC595 that's why I have made it 1.
  • In the UpdateData function, you can see we have to make RCLK Low and after that we have sent our data.
  • But for sending each bit of Data we have to make SRCLK from LOW to High.
  • SER is our Serial IN from Arduino to 74HC595.
  • So, in loop section, I am simply sending HIGH from first Pin to Last and then from last Pin to first and we are getting below results:
  • Now let's have a look at How to connect two 74HC595 chips in parallel to increase the output pins to 16.
  • I have also given these Proteus simulations for download at the end of this tutorial but I would recommend you to design them on your own so that you got better understanding of this shift register.
Arduino 74HC595 Interfacing: 2 Chips in Parallel
  • Now we are gonna place two shift registers in parallel and we will be able to control 16 outputs from single Arduino Pin.
  • Although we are using 3 Arduino Pins but the data is sent through Pin # 6 of Arduino and Pin # 5 and 7 are CLK Pins.
  • Now design a circuit as shown in below figure:
  • Now in Arduino Code, you just need to change the TotalIC to 2 and as you have seen we have already multiplied it with 8 so now our for loop will move from 0 to 15.
  • Pin # 5 and 7 will simply connected to same pins of second shift register but DS will be connected to Q7' of first shift register.
  • Now get your hex file from Arduino software and if everything goes fine then you will get something as shown in below figure:
  • Now let's make it a bit more complex by adding 4 shift registers in parallel.
  • So, design a Proteus Simulation as shown in below figure:
  • We have followed the same principal, Q7' of second chip is connected to DS to 3rd chip and goes on.
  • I have placed these default Pins instead of connecting the wires, it works the same.
  • If this image is not clear then open it in new tab and zoom out to check the connections.
  • Now in your Arduino code, you need to change the TotalIC to 4, as now we are using four chips.
  • Get your Hex File and run Proteus simulation and if everything goes fine then you will get similar results:
  • So, that's How you can quite easily do the Arduino 74HC595 Interfacing and can increase Arduino outputs as much as you want.
  • You can download these Proteus Simulations along with code by clicking the below button:

[dt_default_button link="https://www.theengineeringprojects.com/ArduinoProjects/Arduino 74HC595 Interfacing.rar" button_alignment="default" animation="fadeIn" size="medium" default_btn_bg_color="" bg_hover_color="" text_color="" text_hover_color="" icon="fa fa-chevron-circle-right" icon_align="left"]Download Proteus Simulation & Arduino Code[/dt_default_button]

  • I have also designed this YouTube video to give you a better understanding of Arduino 74HC595 Interfacing:
So, that was all for today. I hope you have enjoyed this Arduino 74Hc595 Interfacing. If you have any questions, then ask in comments and I will try my best to resolve them. In my coming tutorial, I will show you How to increase the Arduino Input Pins. So stay tuned and have fun. :)

2 Relay Module Interfacing with Arduino

Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to provide a detailed discussion on 2 Relay Module Interfacing with Arduino. First of all I would like to explain you that what is relay and how to use it and then we will move forward towards 2 relay module interfacing with Arduino. I have already controlled relay with 555 timers. 2 relay module consists of two relays. Relay is basically an electronic device or a switch which is used to open and close the circuits electronically. A relay controls an electric circuit by opening and closing contacts in another circuit. When the relay contact is normally open (NO), there will be an open connection when the relay is not energized. When the relay contact is normally closed, there will be a closed connection even when the relay is not energized. We can use relays to control the smaller currents in different electronic circuits. 2 relay module has two relays. One relay can control two AC/DC device simultaneously. That means 2 relay module can control four AC/DC devices at a time. 2 relay module is normally used to control the DC motors in different projects e.g. robotics, automation, embedded projects etc. It can control two DC motors simultaneously. Moreover, we can also use it for different applications e.g. to control DC/AC fans, AC/DC lights, AC/DC bulbs and a lot more. The further detail about 2 relay module interfacing with Arduino will be given later in this tutorial.

2 Relay Module Interfacing with Arduino

2 Relay Module is an electronic device consists of two relays as its major components. Relay is a switch which makes or loses the connection between two different circuits. A single relay is capable of controlling two AC/DC devices simultaneously. So, 2 relay module is able to control four AC/DC devices at the same time. Mostly it is used to control the DC motors. It can also be used in different projects e.g embedded projects, robotic, automation, power etc. 2 relay module is shown in the figure given below.
1. Relay Proteus Simulation
2. 2 Relay Module  Components
  • A complete list of the components used while designing 2 relay module is shown in the figure given below.
3. 2 Relay Module  Input Pins
  • 2 relay module has five (5) input pins in total, each perform different action.
  •  All of its pins are provided in the table shown in the figure below.
4. 2 Relay Module  Input Pins Description
  • We must know about the functions of each pin.
  • 2 relay board/module input pin functions are listed in the table shown in the figure below.
  • Both IN1 and IN2 comes from the micro-controller (Arduino UNO in this case).
  • IN1 pin controls the 1st relay attached on 2 relay module.
  • IN2 pin controls the 2nd relay attached on 2 relay module
5. 2 Relay Module  Output Pins
  • 2 relay module has three (3) output pins for each relay.
  • Its output pins are given in the table shown in the figure given below.
6. 2 Relay Module  Output Pins Description
  • Each output pin of 2 relay module has its own functions.
  • 2 relay module pin functions are listed in the table given in the figure shown below.
  • NO pin is normally open pin and device attached to this pin will not work if the relay is not energized.
  • COM is a common pin i.e. ground pin.
  • NC is normally closed pin and device attached to this pin will start working even if the relay is not energized.
7. 2 Relay Module  Compatibility
  • 2 relay module is compatible with different micro-controllers.
  • Some of those micro-controllers are provided in the table shown in the figure given below.
8. 2 Relay Module  Circuit Diagram
  • Circuit diagram of 2 relay module is given in the figure shown below.
9. 2 Relay Module  Interfacing with Arduino Wiring Diagrams
10. 2 Relay Module  Interfacing with Arduino Actual Diagrams
  • I have provided the complete wiring diagram for 2 relay module interfacing with Arduino.
  • Wiring diagram is shown in the figure given below.
11. 2 Relay Module  Interfacing with Arduino Source Code & Description
  • If you are new to Arduino software then you must have a look at How to write Arduino code.
  • You just need to copy and paste the source code given below in your Arduino software.
  • The complete source code for 2 relay module interfacing with Arduino is given below.
int relay1 = 6;
int relay2 = 7;  

void setup() {
  
  pinMode(relay1, OUTPUT); 
  pinMode(relay2, OUTPUT);
}

void loop() {

   digitalWrite(relay1,LOW);
   delay(1000);
 
   digitalWrite(relay1,HIGH); 
   delay(1000);
   
   digitalWrite(relay2,LOW); 
   delay(1000);
   
   digitalWrite(relay2,HIGH); 
   delay(1000);
}
  • First of all I have defined relay pins.
  • Then I have changed the mode of these pins to output.
  • After that I have turned on and off both of the relays with the delay of 1 sec or 1000 msec.
  • So, that was the brief description about the source code for 2 relay module interfacing with Arduino.
  • You can download the wiring diagram and complete Arduino source code here by clicking on the button below.

12. 2 Relay Module  Features
  • The most common features associated with 2 relay module are provided in the table shown in the figure given below.
13. 2 Relay Module  Application
  • 2 relay module applications are given in the table shown in the figure below.
In the tutorial 2 Relay Module Interfacing with Arduino, we have learnt about the components used in the design of 2 relay module. We have also learnt about the 2 relay module interfacing with Arduino. I have provided the complete Arduino source code, you can control this module using the same code. I hope you have enjoyed the tutorial. If you have any problem you can ask us in comments. Out team is 24/7 available for you. I will share different informative engineering topics in my upcoming tutorials. So, till my next tutorial, take care and bye :)

Stepper Motor Speed Control using Arduino

Hello everyone! I hope you all will be absolutely fine and fun. Today, I am going to tell you that how to make a simple algorithm for Stepper Motor Speed Control using Arduino. I have already discussed with you about DC Motor Direction Control using Arduino, Matlab and NI LabVIEW. Moreover, I have also discussed the DC Motor Speed Control using Arduino,Matlab and LabView. If you are working on Stepper Motor, then you must have a look at Stepper Motor Direction Control using Arduino, Stepper Motor Direction Control using Matlab and Stepper Motor Direction Control using NI LabVIEW. Now, in this tutorial I will explain you about the program which will helpful for Stepper Motor Speed Control using Arduino. Before going into the details of this tutorial you must have go through my previous tutorials because I am using the same hardware. So, they will be a lot helpful for the better understanding of this tutorial. In this tutorial I will explain you about making an Arduino program for Stepper Motor Speed Control using Arduino with the help of the serial communication. If the stepper motor is rotating at its maximum speed and you are continuously sending the command through the serial port to reduce its speed, it s speed will be reduced in proportion to the number of command sent through the serial port. Similarly the same procedure will be followed to increase the speed of the stepper motor.

Stepper Motor Speed Control using Arduino

In the tutorial Stepper Motor Direction Control using Arduino, I will explain you about making an algorithm to run the stepper motor at different speed. If the stepper motor is already running at its maximum speed and you want want to accelerate it further then nothing will happen to the speed of the stepper motor. If the stepper motor is rotating slowly and you enhance its speed, then the speed of the motor will increase in proportion to the number of accelerating command sent through the serial port.
  • You can download the complete Arduino source code here by clicking on the button below.

Download Arduino Code

  • Download .rar file, extract it and enjoy the complete source code.
Flow Chart
  • I have made a flow chart so that you can easily understand the entire algorithm because sometimes it becomes difficult to understand the algorithm with the help of the source code.
  • Flow chart for the Stepper Motor Speed Control using Arduino is shown in the figure below.
  • First of all we need to start the serial port so that our communication could be started.
  • Then there is a method to check the speed, if the speed is greater than the maximum speed of the stepper motor then the program will wait for the next command.
  • If the stepper motor is not rotating with its maximum speed then we can increase its speed.
  • Similarly if the minimum speed of the stepper motor is reached then the program will rotate for the next commands.
  • If the minimum limit of the speed of the stepper motor is not reached then we have a option to reduce its further.
  • At the end we should close the serial port so that exchange of unnecessary commands through the serial port could be avoided.
Block Diagram
  • Block diagram will be helpful for use for the better understanding of the exchange of information.
  • It tells us that how the information is exchanged sequentially among all the components used.
  • Block diagram is shown in the figure below.
  • Arduino UNO communicates with the L298 motor controller to control the speed of the stepper motor.
  • L298 Motor controller manipulates the Arduino's commands and starts to control the speed of the stepper motor.
Arduino Code Description
In this section of the tutorial Stepper Motor Speed Control using Arduino, I am going to elaborate you about the Arduino source.
  • I have made two different functions for increasing (accelerating) the speed of the stepper motor and for decreasing (deaccelerating) the speed of the stepper motor respectively.
  • I have declared a variable named as count.
  • In Accelerate function, you have to send the command through the serial port to increase the speed of the stepper motor.
  • In this function, I am continuously increasing the value of the count i.e as many times you send the command the speed of the stepper motor will increase continuously.
  • The source code of the Accelerate function is given below.
   void Accelerate_Motor()
   { 
    count=count+10; //Speed will increase continuously as we continue to press H
    if (count>120)  //Speed must not be greater than 120
    {
      count=120;
      }
    Serial.println("Accelerating"); //printing on the serial port
    Serial.println("");//prints blank line on the serial port
    myStepper.step(stepsPerRevolution);//counter clockwise rotation
    myStepper.setSpeed(count); //Updating the speed of the motor
    lcd.setCursor(3,0);//setting LCD cursor
    lcd.print("Acelerating"); //printing on LCD
   }
  • In Deaccelerate function, you have to send the command through the serial port to increase the speed of the stepper motor.
  • In this function, I am continuously reducing the value of the count i.e as many times you send the command the speed of the stepper motor will reduce continuously.
  • The source code of the Deaccelerate function is given below.
void Deaccelerate()
{
  count=count-10; //reducing the speed of the motor
  if (count<20) //speed of the motor must not be less than 20
  {
    count=20;
    }
  Serial.println("Deaccelerating"); // prints on the serial port
  Serial.println(""); //prints blank line on the serial port
  myStepper.step(stepsPerRevolution);
  myStepper.setSpeed(count); //Updating the speed of the motor
  lcd.setCursor(3,0);  //setting cursor on LCD
  lcd.print("Deaccelerating"); //prints the command on LCD
  }
  • In the main source inside the loop I am calling both of these Accelerate and Deaccelerate functions.
  • The executed commands will also be printed on the LCD (Liquid Crystal Diode).
  • The main source code is given below.
#include <LiquidCrystal.h>//Library for LCD
#include <Stepper.h>     //Library for Stepper motor

const int stepsPerRevolution = 255;  

// initialize the stepper library on pins
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
char data;
int count = 120;
//LCD pins assigning
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
  // set the speed at 60 rpm
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);// rate at which the arduino communicates

lcd.begin(20, 4);//LCD type

lcd.setCursor(3,0);//setting LCD cursor and printing on it
lcd.print("Stepper Motor");
lcd.setCursor(6,1);
lcd.print("Speed");
lcd.setCursor(5,2);
lcd.print("Control");
lcd.setCursor(2,3);
lcd.print("via Arduino UNO");

delay(3000);

lcd.clear ();//Clearing the LCD screen

lcd.setCursor(0,2);
lcd.print("www.TheEngineering");
lcd.setCursor(4,3);
lcd.print("Projects.com");
}

void loop() {
  if(Serial.available())
  {
    data = Serial.read(); //Reading the data from serial port
  }
  
    if(data == 'C'){Clockwise();}      //Clockwise rotation
    if(data == 'A'){AntiClockwise();} //Anti-clockwise rotation
    if(data == 'S')                  //stopping the stepper motor
    {
      data = 0; 
      lcd.setCursor(3,0);
      lcd.print("No rotation");
      Serial.println("No rotation");//print on the serial
      }   
     if(data == 'H'){Accelerate_Motor();}
     if(data == 'L'){Deaccelerate();}
}
Complete Hardware Setup
  • In this section of the tutorial, I will show you the complete hardware setup that I have used for this project.
  • Hardware consists of 12V power supply, Arduino UNO, L298 motor controller.
  • When you upload the code to the Arduino board the system will look like the figure shown below.
  • When you press to increase the speed of the stepper motor, the statement accelerating will be printed on the LCD.
  • The printed executed command is printed on the LCD and is shown in the figure below.
  • When you press to reduce the speed of the stepper motor, the statement Deaccelerating will be printed on the LCD.
  • The printed executed command is printed on the LCD and is shown in the figure below.
That is all from the tutorial Stepper Motor Speed Control using Arduino. I hope you all have enjoyed this tutorial. If you face any sort of problem regarding anything you can ask me anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way if possible. I will explore Arduino by making further projects and I will share them with all of you as well in my later tutorials. So, till then, Take Care :)

Stepper Motor Direction Control using Arduino

Hello friends! I hope you all will be absolutely fine and having fun. Today, I will elaborate you that how can we make a simple algorithm for Stepper Motor Direction Control using Arduino. In my previous tutorials I made algorithm for DC Motor Direction Control using Arduino, DC Motor Direction Control using Matlab, DC Motor Speed Control using Arduino and DC Motor Speed Control using Matlab. Now, in this tutorial I will control a stepper motor using Arduino by entering the different commands through its serial port. Before going into the detail of this tutorial, you must know the basic difference between stepper and DC motors. DC motors have only two input terminal one is positive and the other one is negative. You just have to provide the power supply and it will start rotating but this is not the case in stepper motor. The stepper motor which I will use in this tutorial, has six pins out of which four pins provide pulses or steps and the other two pins are power pins. So, in this tutorial I will control this six pins stepper motor using L298 motor controller and Arduino UNO board. Basically we can use stepper motor where precision is required. Stepper motor has wide range of applications e.g robotics, CNC machines, home automation etc. In simple word, we can say that stepper motor can be used where there is a need to move at particular angle. So, let's get started with Stepper Motor Direction Control using Arduino:

Stepper Motor Direction Control using Arduino

In this tutorial we will learn how to make a program for Stepper Motor Direction Control using Arduino by sending dfferent commands from the serial port. First of all, I am going share the list of components used for this mini project.
  • Arduino UNO
  • Stepper motor (6 wire)
  • L298 Motor Controller (H-Bridge)
  • Voltage Regulator (7805)
  • 1000uF
  • Jumper Wires
  • Solderig Iron
  • Soldering Wire
I want to tell you a bit about the stepper motor because all the other components are discussed in detail in DC Motor Direction Control using Arduino.
Stepper Motor
Basically, stepper motors are like the DC motors that rotate in discrete steps. They have multiple arranged coils and they are usually known as phases. Motor will rotate one step at a time if we energize each phase sequence. High levels of precision can be achieved by controlling the stepper motor with computer. Steppers motors are available in the market in many different sizes. The speed of the stepper motor is controlled by frequency of pulses generated. They have wide range of applications like hard disk drives, robotics, telescope, antenna, toys etc. A six wire stepper motor is shown in the figure below.  
  • You can download complete source code for Stepper Motor Direction Control using Arduino by clicking the below button:

Download Arduino Source Code

Selection of Wires
  • I have used 6 wire stepper motor and each wire has its own function.
  • I have first divided these six wires into two pair.
  • Each pair is consisting of three wires out of which one wire is common and the other two generate pulses.
  • The two pair of three wires are shown in the figure below.

  • Then, I have chosen a common wire in each pair from which the resistance to the other two wires is common.
  • I have checked the resistance from the common wire to the both of the other wires of the same pair.
  • I found that the resistance from the common wire to both of the other wires is same.
  • We can see in the figure above the blue, pink and white wires belong to the same pair out of which white is a common wire.
  • Here is the screen shot of the figure when I found the resistance between white and blue wire and I found it to be 8.0 ohms.
  • The screen shot of the above steps is shown in the figure below.
  • After that. I checked the resistance between white and pink wire and found it to be 8.1 which is almost the same as 8.0 so, this shows that the white wire is common to both of the blue and pink wire.
  • Here is the screen shot of the above step.
  • Then I found the resistance between pink and blue wire and it was 15.6 which is exactly the double of the earlier resistance.
  • It is shown in the figure below.
  • I have connect the both common wires as shown in the figure below.

  • Here's the video in which I have discussed it in detail How to identify the wires of Stepper Motor:
  • The remaining four wires are used to generate pulses which are also know as steps
  • I have connected theses four wires to the output pins OUT1, OUT2, OUT3 and OUT4 of the L298 micro controller.
  • Input pins of L298 micro controller In1, In2, In3 and In4 are connected to the pin no 7, 6, 5 and of the Arduino UNO's board respectively.
Note:

I have also controlled the stepper motor using PIC micro controller so I would suggest all of you to first go through that tutorial before going into the details of this tutorial.

Block Diagram
  • I have made a simple block diagram for Stepper Motor Direction Control using Arduino, which will be helpful to clearly understand the algorithm and the assembling of the components of Stepper Motor Direction Control using Arduino.
  • The screenshot of the block diagram is shown in the figure below.
  • First of all we need a power supply to run the project properly.
  • Arduino reads the commands from the serial port and sends to the L298 motor driver to rotate the stepper motor.
  • The commands got printed on the LCD (Liquid Crystal Display).
Arduino Source Code Description
  • The main function of the Stepper Motor Direction Control using Arduino is given below.
#include <LiquidCrystal.h>//Library for LCD
#include <Stepper.h> //Library for Stepper motor

const int stepsPerRevolution = 255;  

// initialize the stepper library on pins
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
char data;
//LCD pins assigning
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
  // set the speed at 60 rpm
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);

lcd.begin(20, 4);//LCD type

lcd.setCursor(3,0);//setting LCD cursor and printing on it
lcd.print("Stepper Motor");
lcd.setCursor(5,1);
lcd.print("Direction");
lcd.setCursor(5,2);
lcd.print("Control");
lcd.setCursor(2,3);
lcd.print("via Arduino UNO");

delay(3000);

lcd.clear ();//Clearing the LCD screen

lcd.setCursor(0,2);
lcd.print("www.TheEngineering");
lcd.setCursor(4,3);
lcd.print("Projects.com");
}

void loop() {
  if(Serial.available())
  {
    data = Serial.read(); //Reading the data from serial port
  }
  
    if(data == 'C'){Clockwise();}//Clockwise rotation
    if(data == 'A'){AntiClockwise();}//Anti-clockwise rotation
    if(data == 'S')//stopping the stepper motor
    {
      data = 0; 
      lcd.setCursor(3,0);
      lcd.print("No rotation");}
    
}
  • In the code given above we have first initialized the LCD and Stepper motor libraries.
  • Then, I assigned stepper motor pins at which it is connected to the Arduino.
  • After that I initialized the LCD pins at which it is connected to Arduino UNO.
  • Then I have made three different if statements, for the clockwise, for the anti clockwise rotation and for the no rotation.
  • Then in the loop I called clock wise and anti clockwise functions whose source code will be give and explained below.
  • Then, I cleared the serial data in order to stop the rotation of the motor.
  • The source code of the clockwise function is given below.
void Clockwise()//function for clockwise rotation
{
    Serial.println("clockwise"); //printing on the serial port
    Serial.println("");//prints blank line on the serial port
    myStepper.step(stepsPerRevolution);//counter clockwise rotation
    lcd.setCursor(3,0);//setting LCD cursor
    lcd.print("Clockwise"); //printing on LCDa
}
  • The source code for the anti clockwise function is given below.
void AntiClockwise()//function for anti clockwise rotation
{
  Serial.println("anti-clockwise");//print on the serial
  Serial.println("");//prints a blank line on the serial
  myStepper.step(-stepsPerRevolution);//clockwise movement
  lcd.setCursor(3,0);//setting LCD cursor
  lcd.print("Anti-clockwise");//printing on LCD
}
  • Now, open your Arduino software, just copy and paste the source code given above.
  • Run the program and open the Serial Port at the top right of the Arduino software.
  • Now, when you enter the command stepper motor will start running in clockwise direction.
  • If you send the command through the serial port stepper motor will start to rotate in counter clockwise direction.
  • If you send the command the rotation of the stepper motor will be stopped.
Actual Hardware Setup
  • The actual hardware operating setup for Stepper Motor Direction Control using Arduino is given in the figure below:
  • Now, if you send the command  through the serial port the stepper motor will start to rotate in clockwise direction and the command will also be printed on the LCD.
  • The screenshot of the printed command on LCD is shown in the figure below.
  • Now, if you send the command  through the serial port the stepper motor will start to rotate in anti clockwise direction and the command will also be printed on the LCD.
  • The screenshot of the printed command on LCD is shown in the figure below.
  • Now, if you send the command  through the serial port the stepper motor will show no more rotation and the command will also be printed on the LCD.
  • The screenshot of the printed command on LCD is shown in the figure below.
  • Here's the complete video demonstration of Stepper Motor Direction Control using Arduino, I hope it will help as well:
That's all from the tutorial Stepper Motor Direction Control using Arduino. I hope you enjoyed this tutorial. If you face any sort of problem, you can ask me anytime without feeling any kind of hesitation. I will try my level best to solve your problem in a better way if possible. I will explore Arduino by making different projects on it. Till then, Take care :)

How to write Arduino code ?

Hello everyone, I hope you all are fine and having fun. In today's tutorial, I am going to show you How to write Arduino code. In the previous tutorial, we have seen the Simple Arduino LED Example so if you haven't read that tutorial then I must suggest you to read it first because I am gonna use the same simulation with some advancements in it. Moreover, you should also have a look at How to do Arduino Serial Communication because we are also gonna use Serial Port in today's tutorial and one more tutorial which you must read is How to use digitalRead in Arduino because we are dealing with digital pins here. So, I hope that you have read those tutorial and are ready to learn How to write Arduino code. So, let's have a look at How to write Arduino Code:

How to write Arduino code ?

  • In the previous tutorial named as Arduino LED Example, we have designed a simulation in which I have made some changes and added few buttons in it.
  • This new modified simulation is shown in below figure:
  • You can see in the above figure that I have connected LEDs with all the digital Pins and Logic State with all the analog pins and a Virtual Terminal is connected with Pin # 0 and 1 of Arduino.
  • You can download the complete simulation with Proteus code for this tutorial How to write Arduino Code by clicking the below button:

Download Simulation & Code

Note:
  • We can also use Analog Pins as digital and that's what we are gonna do in today's tutorial.
  • So, that's why I have placed digital logic state which is actually acting as a button here.
  • Moreover, if you haven't worked with Virtual Terminal then you should read How to use virtual Terminal in Proteus.
  • If you have a look at the previous code then you must have remembered that it was quite lengthy and I have mentioned that we will make it efficient in the next tutorial.
  • So, now let's make a small code in which we will blink these LEDs one by one.
  • But before going any further, you must first read Getting Started with Arduino Programming so that you also know the basics of Arduino Programming structure.
  • So, now I am going to make a small function which I will call in the Main loop function every time I need to blink the LED.
  • So, that lengthy code is now gonna compress to small code and is given below:
// ===== It's the First Version ========

int Led1 = 13;
int Led2 = 12;
int Led3 = 11;
int Led4 = 10;
int Led5 =  9;
int Led6 =  8;
int Led7 =  7;
int Led8 =  6;
int Led9 =  5;
int Leda =  4;
int Ledb =  3;
int Ledc =  2;

int Led = 0;

void setup() 
{
    pinMode(Led1, OUTPUT);
    pinMode(Led2, OUTPUT);
    pinMode(Led3, OUTPUT);
    pinMode(Led4, OUTPUT);
    pinMode(Led5, OUTPUT);
    pinMode(Led6, OUTPUT);
    pinMode(Led7, OUTPUT);
    pinMode(Led8, OUTPUT);
    pinMode(Led9, OUTPUT);
    pinMode(Leda, OUTPUT);
    pinMode(Ledb, OUTPUT);
    pinMode(Ledc, OUTPUT);
}

void loop() 
{
    LedBlinkFunction(1);   
    delay(1000);
    LedBlinkFunction(2);   
    delay(1000);
    LedBlinkFunction(3);   
    delay(1000);
    LedBlinkFunction(4);   
    delay(1000);
    LedBlinkFunction(5);   
    delay(1000);
    LedBlinkFunction(6);   
    delay(1000);
    LedBlinkFunction(7);   
    delay(1000);
    LedBlinkFunction(8);   
    delay(1000);
    LedBlinkFunction(9);   
    delay(1000);
    LedBlinkFunction(10);   
    delay(1000);
    LedBlinkFunction(11);   
    delay(1000);
    LedBlinkFunction(12);   
    delay(1000);
    LedsOFF();
    delay(1000);
}

void LedBlinkFunction(int LedNo)
{
    if(LedNo == 1){Led = Led1;}
    if(LedNo == 2){Led = Led2;}
    if(LedNo == 3){Led = Led3;}
    if(LedNo == 4){Led = Led4;}
    if(LedNo == 5){Led = Led5;}
    if(LedNo == 6){Led = Led6;}
    if(LedNo == 7){Led = Led7;}
    if(LedNo == 8){Led = Led8;}
    if(LedNo == 9){Led = Led9;}
    if(LedNo ==10){Led = Leda;}
    if(LedNo ==11){Led = Ledb;}
    if(LedNo ==12){Led = Ledc;}
   
    digitalWrite(Led, HIGH);
}

void LedsOFF()
{
    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW);   
}
  • You can see the above code is totally different from the one we have used in Arduino LED Example.
  • In the above code I have created two functions named as LedBlinkFunction(int LedNo) and LedsOFF().
  • So, that way, I have made the code short as well as efficient.
  • So, now add this code in your Arduino sofware and Get your Arduino Hex File.
  • Upload this hex file in Proteus and if everything goes fine then you will get results as shown in below figure:
  • The abovecode is quite small as compared to the previous one but let's make it more short and efficient.
  • Now, I am gonna use the For Loop which I haven't used before and that way I don't need to call that function every time instead I will just call it in For Loop so let's have a look at the below code:
// ===== It's the Second Version ===========

int Led1 = 13;
int Led2 = 12;
int Led3 = 11;
int Led4 = 10;
int Led5 =  9;
int Led6 =  8;
int Led7 =  7;
int Led8 =  6;
int Led9 =  5;
int Leda =  4;
int Ledb =  3;
int Ledc =  2;

int Led = 0;

void setup() 
{
    pinMode(Led1, OUTPUT);
    pinMode(Led2, OUTPUT);
    pinMode(Led3, OUTPUT);
    pinMode(Led4, OUTPUT);
    pinMode(Led5, OUTPUT);
    pinMode(Led6, OUTPUT);
    pinMode(Led7, OUTPUT);
    pinMode(Led8, OUTPUT);
    pinMode(Led9, OUTPUT);
    pinMode(Leda, OUTPUT);
    pinMode(Ledb, OUTPUT);
    pinMode(Ledc, OUTPUT);
}

void loop() 
{
    for(int x = 1; x < 13; x++)
    {
        LedBlinkFunction(x);   
        delay(1000);
    }
    
    LedsOFF();
    delay(1000);
}

void LedBlinkFunction(int LedNo)
{
    if(LedNo == 1){Led = Led1;}
    if(LedNo == 2){Led = Led2;}
    if(LedNo == 3){Led = Led3;}
    if(LedNo == 4){Led = Led4;}
    if(LedNo == 5){Led = Led5;}
    if(LedNo == 6){Led = Led6;}
    if(LedNo == 7){Led = Led7;}
    if(LedNo == 8){Led = Led8;}
    if(LedNo == 9){Led = Led9;}
    if(LedNo ==10){Led = Leda;}
    if(LedNo ==11){Led = Ledb;}
    if(LedNo ==12){Led = Ledc;}
   
    digitalWrite(Led, HIGH);
}

void LedsOFF()
{
    for(int x = 2; x < 13; x++)
    {
        digitalWrite(x, LOW);
    }  
}
  • Now, you can see in the above code that I have used the For Loop in Main Loop function as well as in LedsOFF() Function.
  • And you can see the code has become quite small and more understanding.
  • The result of this code is exactly the same as the First Code.
  • Now let's have a look at those switches, I will design another code in which I will add different LEDs routines on each button press.
  • Like if you press the first button then it will start from top and if you press the second button then it will start from bottom and similar functions on other buttons.
  • So, here's the code which you need to add in your Arduino:
int Led1 = 13;
int Led2 = 12;
int Led3 = 11;
int Led4 = 10;
int Led5 =  9;
int Led6 =  8;
int Led7 =  7;
int Led8 =  6;
int Led9 =  5;
int Leda =  4;
int Ledb =  3;
int Ledc =  2;

int Led = 0;

int Button1 = A0;
int Button2 = A1;
int Button3 = A2;
int Button4 = A3;
int Button5 = A4;
int Button6 = A5;

void setup() 
{
    Serial.begin(9600);
    pinMode(Led1, OUTPUT);
    pinMode(Led2, OUTPUT);
    pinMode(Led3, OUTPUT);
    pinMode(Led4, OUTPUT);
    pinMode(Led5, OUTPUT);
    pinMode(Led6, OUTPUT);
    pinMode(Led7, OUTPUT);
    pinMode(Led8, OUTPUT);
    pinMode(Led9, OUTPUT);
    pinMode(Leda, OUTPUT);
    pinMode(Ledb, OUTPUT);
    pinMode(Ledc, OUTPUT);

    pinMode(Button1, INPUT_PULLUP);
    pinMode(Button2, INPUT_PULLUP);
    pinMode(Button3, INPUT_PULLUP);
    pinMode(Button4, INPUT_PULLUP);
    pinMode(Button5, INPUT_PULLUP);
    pinMode(Button6, INPUT_PULLUP);
}

void loop() 
{
    if(digitalRead(Button1) == HIGH)
    {
        for(int x = 1; x < 13; x++)
        {
            LedBlinkFunction(x);   
            delay(1000);
        }
        
        LedsOFF();
        delay(1000);
    }

    if(digitalRead(Button2) == HIGH)
    {
        for(int x = 13; x > 0; x--)
        {
            LedBlinkFunction(x);   
            delay(1000);
        }
        
        LedsOFF();
        delay(1000);
    }

    if(digitalRead(Button3) == HIGH)
    {
        LedsON();
        delay(1000);
        LedsOFF();
        delay(1000);
    }

    if(digitalRead(Button4) == HIGH)
    {
        Serial.print("www.TheEngineeringProjects.com");
        while(digitalRead(Button4) == HIGH);
    }

    if(digitalRead(Button5) == HIGH)
    {
        if(Serial.available())
        {
            char data = Serial.read();
            data = data - 49;
            digitalWrite(data, HIGH);
        }
    }

    if(digitalRead(Button5) == HIGH)
    {
        
    }
}

void LedBlinkFunction(int LedNo)
{
    if(LedNo == 1){Led = Led1;}
    if(LedNo == 2){Led = Led2;}
    if(LedNo == 3){Led = Led3;}
    if(LedNo == 4){Led = Led4;}
    if(LedNo == 5){Led = Led5;}
    if(LedNo == 6){Led = Led6;}
    if(LedNo == 7){Led = Led7;}
    if(LedNo == 8){Led = Led8;}
    if(LedNo == 9){Led = Led9;}
    if(LedNo ==10){Led = Leda;}
    if(LedNo ==11){Led = Ledb;}
    if(LedNo ==12){Led = Ledc;}
   
    digitalWrite(Led, HIGH);
}

void LedsOFF()
{
    for(int x = 2; x < 14; x++)
    {
        digitalWrite(x, LOW);
    }  
}

void LedsON()
{
    for(int x = 2; x < 14; x++)
    {
        digitalWrite(x, HIGH);
    }  
}
  • Now when you upload the code, you will get the similar results but now you must press the buttons and you will see different functionalities of LEDs.
  • The results are given in the below video:
I hope you have enjoyed How to write Arduino code and are gonna get help from it. That's all about How to write Arduino code. So, will meet you guys in the next tutorial. Take care and have fun !!! :)

A Simple Arduino LED Example in Proteus

Hello friends, I hope all are fine and having fun with your projects. We have covered enough Arduino commands in this Arduino Tutorial for Beginners series and now we are ready to create a simple project by interfacing an LED (Light Emitting Diode). Today, I am going to share a very Simple Arduino LED Example in Proteus ISIS. First I will blink single LED using Arduino UNO and then I will blink multiple LEDs in Proteus. When you start working on Arduino then Arduino LED example is the first example which you must try because its the easiest one. Moreover, we all know that we have a small LED connected to pin # 13 on each Arduino so you can also check your Arduino as well that whether its working or not. So, let's get started with Simple Arduino LED Example in Proteus ISIS:

A Simple Arduino LED Example in Proteus

  • You can download, all the simulation files and codes for Arduino LED examples used in this tutorial, by clicking the below button:

Download Simulation Files

  • First of all, design a simple circuit of Arduino LED in Proteus ISIS as shown in below figure:
  • Now as you can see in the above figure that I have used an LED on Pin # 13 of Arduino UNO.
  • So, now upload the below sketch in your Arduino, its the Blink Example from Arduino, which works perfect for this Arduino LED Example:
void setup() {
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}
  • The above code is quite simple and you can see first we have used the pinMode Arduino Command to make the LED pin Output.
  • After that, we have used Arduino digitalWrite Command to blink the LED.
  • Now get the hex file from Arduino software and add it in your Proteus Arduino board.
  • Once the hex file is uploaded in the Arduino then run your Arduino LED Proteus Simulation and if everything goes fine then your LED will start blinking as shown in below figure:
  • Now you can see in the above figure that our LED at Pin # 13 started blinking.
  • If you read the above code of Arduino LED exmaple then its quite simple, first of all I just make the Pin # 13 output and then I have made it HIGH and LOW with a delay of 1000 msec.
  • You might wanna read How to use digitalRead in Arduino that will give you a better idea of How to deal with any digital pin.
  • So, now let's add more LEDs on other digital Pins of Arduino.
  • So, design a simulation as shown in the below figure:
int Led1 = 13;
int Led2 = 12;
int Led3 = 11;
int Led4 = 10;
int Led5 =  9;
int Led6 =  8;
int Led7 =  7;
int Led8 =  6;
int Led9 =  5;
int Leda =  4;
int Ledb =  3;
int Ledc =  2;

void setup() 
{
    pinMode(Led1, OUTPUT);
    pinMode(Led2, OUTPUT);
    pinMode(Led3, OUTPUT);
    pinMode(Led4, OUTPUT);
    pinMode(Led5, OUTPUT);
    pinMode(Led6, OUTPUT);
    pinMode(Led7, OUTPUT);
    pinMode(Led8, OUTPUT);
    pinMode(Led9, OUTPUT);
    pinMode(Leda, OUTPUT);
    pinMode(Ledb, OUTPUT);
    pinMode(Ledc, OUTPUT);
}

void loop() 
{
    digitalWrite(Led1, HIGH);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW);    
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2, HIGH);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3, HIGH);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4, HIGH);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5, HIGH);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6, HIGH);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7, HIGH);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8, HIGH);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9, HIGH);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda, HIGH);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb, HIGH);
    digitalWrite(Ledc,  LOW); 
    delay(1000);

    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc, HIGH); 
    delay(1000);
}
  • Upload this hex file in your Proteus Arduino and then run your simulation.
  • If everything goes fine then you will get all your LEDs blinking.
  • I have shown a glimpse of its working in below figure:
  • So, download the files and run your simulation and test it out.
  • If you check the code then it seems quite lengthy but its very simple.
  • I am just keeping one LED on and others OFF.
  • Now, let me tell you one thing, this is not the best way of coding but for starters you should first try it out.
  • In the coming lecture, I will teach you How to write Arduino Code Efficiently like I don't wanna add 100 lines just for such small work.
So, that's all for today. I hope you have enjoyed today's Arduino LED Example and are gonna test it. So, see you in next tutorial. Take care !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir