Friday, October 26, 2012

Arduino 3 Bit Counter with code and schemetics

Hi guys,

In this blog, I am going to show how to make 3 Bit counter using Arduino with Code and Schematics.
For this you'll need Arduino Uno R3. If you don't have one, you can buy it from below link.
https://amzn.to/2QRr2IW


For making 3 bit up counter we need to first understand desired outputs.
The output for 3 bit up counter will start from 000 to 111.

000 -->> 001 -->> 010 -->> 011 -->> 100 -->> 101 -->> 110 -->> 111 -->> 000.

So for circuit, make the connection shown in the figure with arduino board. Connect digital pin 3,4 and 5 to LED via 1k register. Here make sure you connect LEDs via register. If you will not connect register, there may be possibility of high current through LED, which may result in short circuit the board.



Schematics of Arduino UNO R3 connected with LEDs

Open an arduino sketch from clicking new in file menu. To write code for 3 Bit up-Counter first, understand the algorithm.
We are sure that we must need to use for loop which will start from 0 and it will go in loop for 8 times. Because for 3 bit maximum possible way for counter = 2^3 = 8.
So for loop will go from 0 to 7 i.e. 8 times.

Now MSB is yellow LED in schematics. That is Pin 5 is MSB and pin 3 is LSB. 
For up counter when the number is odd (i.e. 1,3,5 and 7) LSB will be 1. otherwise LSB = 0.
Same way we can design for MSB and middle bit also. The stat of pin is shown for each 
value of possible output.


                   Pin 5            Pin 4             Pin 3
0  -  000   -     0                  0                  0
1  -  001   -     0                  0                  1
2  -  010   -     0                  1                  0
3  -  011   -     0                  1                  1
4  -  100   -     1                  0                  0
5  -  101   -     1                  0                  1
6  -  110   -     1                  1                  0
7  -  111   -     1                  1                  1


So we should put if and else statements by seeing in table.
From above table it is easy to say that Pin 5 is 1 if value is 4,5,6,7 (i.e. value>=4)
Pin 4 is 1 if value is 2,3,6,7
Pin 3 is 1 if value is 1,3,5,7 (i.e. odd values)

So final code will be like:


int ledPin1=3;   // IC Pin no 3
int ledPin2=4;
int ledPin3=5;

void setup()   // For initial Setup
{
 pinMode(ledPin1,OUTPUT);   // Select the pin as output pin
 pinMode(ledPin2,OUTPUT);
 pinMode(ledPin3,OUTPUT);
}

void loop()
{
 for(int i=0;i<=7;i++)
 {
  if(i==2 || i==4 || i==6 || i==0)
  {
   digitalWrite(ledPin1,LOW);  // Pin no 3 or led 1 low when even, not glow
  }
         else
  {
   digitalWrite(ledPin1,HIGH);
  }
  if(i==2 || i==3 || i==6 || i==7)
  {
   digitalWrite(ledPin2,HIGH);  //Pin no 4 or LED 2 High -> Glow
  }
  else
  {
   digitalWrite(ledPin2,LOW);
  }
  if(i>3)
  {
   digitalWrite(ledPin3,HIGH);  //Pin 5 or LED 3 HIGH -> Glow
  }
  else
  {
   digitalWrite(ledPin3,LOW);
  }

  delay(1500);       // to wait for 1500 milliseconds
 }
}



Here you can see the circuit in progress. MSB is upper most LED. And LSB islower most LED.


I hope you will try this... Enjoy guys...
Thank you...

No comments:

Post a Comment