Shop

I sell on Tindie

Tuesday, February 15, 2022

Binary up and down counter - Arduino

 

Arduino Code:

So you want to know how to build a binary counter using an Arduino microcontroller? In this tutorial you will learn how to count up and count down using two push buttons and eight leds. *You will also need resistors to prevent the leds from burning out (shame on me for not including them in the circuits)
I will not go over every detail but the code will be provided so that you can build your own counter. I will try my best to explain the working of this circuit...here goes.
First start by opening your programming software. I used  Arduino Software.  The beginning of the code defines the led pins and push buttons. My code used pin 2 & 3 for the up and down counter (known as interrupt pins on the Arduino Uno & a few other boards).  The buttons are then set to HIGH so that when they are pushed, the state is changed to LOW.(Pull-up resistor)

For the eight binary pins use pins (5-12).  Also included is a variable to store the number of times a button is pushed. This is very important for how the micro controller works to output the binary number. With that in mind debouncing the buttons are essential in correctly identifying when in fact there was or was not a button press which increments and decrements the counter. 
In the setup the stateChangeDetection (int state, int = i , and int = 0) is telling the program the state will either be HIGH or LOW and then whether or not to start on HIGH or LOW. It then uses this declared state throughout the remainder of the program.

The attachInterrupt syntax is as follows attachInterrupt(pin, ISR, mode) ;  please read more about it at the link provided.  

 The i++ will increment the counter i.e 0000 0000 becomes 0000 0001 when the up button is pressed and back to 0000 0000 if the down counter is pressed. The code loop basically converts the number of button pushes, stores it as a string, gets the length of the string and then outputs it to the leds. Please note that for convenience I choose the the red led for the LSB (least significant bit).
binarymakecounterhw.ino
Download File

Breadboard Setup:

The setup of the breadboard is as follows:
  1. Ground the left side of the button (wire the bottom left pin to ground) repeat step one for the second button.
  2. The top right pin of the button should connect to 3 (count up) and pin 2 (count down)
  3. Ground each led pin. (flat side)
  4. Starting with the red led connect the anode (+) side to pin 5 *include a resistor(680 ohms is ideal). Repeat steps until all leds are connected to the Arduino.
  5. Be sure to add jumpers across the board bus lines to have power down both strips of the prototype bread board. I used the 3.3V and GND for the voltage supply.
I hope that you were able to follow these steps to successfully build or modify your own circuit. Please leave a comment for question, concerns, or thank you's.

Please Visit again soon. Check out more projects before you go!

No comments:

Post a Comment

Add your opinion. What’s your main takeaway from the blog post?

Note: Only a member of this blog may post a comment.

About Raspberry Pi Pico W (WiFi Onboard)

July 1st 2022 💖We love new arrivals! The  Raspberry Pi Pico W  is a new tiny platform based on the  RP2040  silicon and it provides connect...

Popular Post