Basic Binary Buttons Backed by a Breadboard! (A Simple Circuit From Binary to Decimal)
by johnsonb25 in Circuits > Arduino
718 Views, 2 Favorites, 0 Comments
Basic Binary Buttons Backed by a Breadboard! (A Simple Circuit From Binary to Decimal)
In this project, we’ll create a simple yet effective Arduino system that reads button presses, stores them as binary values, and converts those values into a decimal format. This project is great for beginners looking to explore basic input handling with Arduino, as well as for anyone interested in digital systems and binary counting. Get ready to learn some circuitry!
Objectives for this project:
- Learn how to read button inputs through Arduino software
- Investigate storing values into arrays on press
- Convert the binary input into the decimal value as an output and show it on screen
Supplies
Materials for this project are cheap and easy. All you will need is (If you buy a beginner Arduino kit you will likely have all of the materials inside):
- Arduino Board (I use an Arduino Uno R3)
- 2 Push Buttons
- 10k Ohm Resistors
- Breadboard
- Jumper Wires
- USB Cable for programming the Arduino
- Arduino IDE installed on your computer (its free!)
Circuit Diagram
Before looking at any code we need to build the circuit! Grab all of the components and construct as seen in the image.
- Begin by connecting a wire from the 5V connection on the Arduino to the + rail on the breadboard
- Do the same with the GND port on Arduino to the - rail on the bread board
Now we will begin placing all of the key components into the circuit. (Rails are lines on the breadboard. The + and - ones are connected vertically all the way down the breadboard. The numbered ones are connected across horizontally)
- Place the buttons across the middle of the breadboard such that two legs are in the a-e side and 2 legs are in the f-j side.
- This gives us as much space as possible to work with
- The specific line you place these into does not matter, but I placed mine in the 2/4 rails and the 7/9 rails.
- Take your two 10k Ohm Resistors and place one leg into the + rail and the other into either the 2 rail or 7 rail (or whatever rail that correlates to for you)
- Take a couple more wires and go from the other rails (4 and 9 for me) and connect them into the - rail.
Congratulations! You completed a full circuit! However, this will not read anything because the Arduino cannot talk with the buttons yet, so let's fix that.
- Take a wire and connect from the rail with the resistor (2 or 7 for me) and connect to one of the digital pin connections on the Arduino, I placed mine into pin 7 and pin 8.
- We use digital pins because they take 2 states HIGH and LOW which is all we need for this circuit!
- Repeat with the other button so that you have two wires connected from the breadboard into digital pins on the Arduino.
TaDa! You are now ready to move onto the next step!
Setting Up the Code
In this step we will begin laying the groundwork for the code that you will need to upload into the Arduino. Start by opening up Arduino IDE and create a new sketch. Below is the code that I use and I will explain what it means under. Feel free to download the code as a file or copy and paste it into your IDE software!
----------------------------------------------------------------------------------------------------------------------------------------------------------
const int buttonPins[2] = {7, 8}; // Button pin numbers
int buttonStates[2]; // To hold button states
int binaryValues[8]; // Array to store the last 8 button press values
int currentPressIndex = 0; // Index to track the current press
unsigned long lastDebounceTime[2] = {0, 0}; // Last time button state was updated
const unsigned long debounceDelay = 200; // Debounce time in milliseconds
void setup() {
Serial.begin(9600); // Initialize serial communication
for (int i = 0; i < 2; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Set button pins as inputs with internal pull-up resistors
}
}
void loop() {
// Read the state of each button
for (int i = 0; i < 2; i++) {
int reading = digitalRead(buttonPins[i]);
// Check for button state change and debounce
if (reading == LOW && (millis() - lastDebounceTime[i]) > debounceDelay) {
// Store the button press in the array based on which button is pressed
if (currentPressIndex < 8) {
binaryValues[currentPressIndex] = (i == 0) ? 1 : 0; // Store 1 for button 7, 0 for button 8
currentPressIndex++;
lastDebounceTime[i] = millis(); // Update the last debounce time
}
// Print after 8 button presses
if (currentPressIndex == 8) {
Serial.print("Last 8 Button Presses: ");
for (int j = 0; j < 8; j++) {
Serial.print(binaryValues[j]);
if (j < 7) {
Serial.print(", ");
}
}
Serial.println();
// Convert the binary values to decimal
int decimalValue = binaryToDecimal(binaryValues, 8);
Serial.print("Decimal Value: ");
Serial.println(decimalValue);
// Reset index for the next set of presses
currentPressIndex = 0;
}
}
}
}
// Function to convert binary array to decimal
int binaryToDecimal(int bits[], int length) {
int decimalValue = 0;
for (int i = 0; i < length; i++) {
decimalValue += bits[i] * pow(2, length - 1 - i);
}
return decimalValue;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Downloads
Explaining the Code
Wow that is a lot of code, but what does it mean? Let's start with the basics. There are different ways of initializing constants and variables for Arduino.
- const - means that this is a variable that cannot be reassigned later in the code
- int - means the data stored is an integer input (0, 1, 2, 3)
- unsigned - means that the data inside the variable can never be negative. Think of it like an absolute value almost.
- long - just means the data structure can hold a lot more values than a measly integer
What are the variables we use though?
- buttonPins - This is where we tell Arduino what digital pins the buttons are connected to
- binaryValues - When a button is pressed this will either populated a position in the array with a 1 or a 0 based on the button pressed
- currentPressIndex - This reminds us where in the array we are because after 8 presses we are ready to show the number
- lastBounceTime - This is a debug parameter that is storing the time it took for the last press so that it does not have double entries from one press
- debounceDelay - This gives us some time between pressing, so the button can debounce and allow the circuit to complete itself again.
The Setup Function:
- We begin by telling the Arduino to communicate with us at a rate of 9600 baud which means there will be 9600 readings every second
- Then we set the button pins as inputs with internal resistors (this is because we threw in the 10k Ohm resistors in the circuit)
The Loop Function:
- This is the continuous function that will last for all of eternity unless we tell it to stop
- First it will continuously read the state of the buttons.
- Then it checks if the button is pressed and if it’s stable (debounced).
- If the button is pressed, it records the button press in the binary array.
- Once 8 presses are recorded, it prints the button presses and the corresponding decimal value.
Sounds simple enough, but what is that little bit at the end? I'm glad you asked, it is the Binary to Decimal Function! This is where the real magic happens. We need to take the input from each of the positions in the array and make sure it fits to the decimal value. The far-right digit is associated with 0 or 1, but if you want a value of 2, then you need the second from right to be a 1. This gets confusing but each position is related to a power of 2 minus 1. For our system we have 8 bits, so we can have up to 2^8 -1 or 255 values!
Testing the System
Now that we have the code, and the circuit complete let's put it all together! Plug the Arduino into the computer and press the arrow that says upload in the top left corner. It will compile/verify the code first (to make sure there are no errors or bugs) and then it will upload to the Arduino. Next click the button in the top right corner called "Serial Monitor" this is where the data will print out. Now click away! Press one of the buttons 8 times and see what happens. You should see the sequence displaced followed by the value associated with it!
Conclusion and Further Improvements
In this project, you successfully built an Arduino system to read button presses, store them as binary values, and convert them to decimal. This project serves as a fundamental exercise in understanding digital inputs, binary counting, and serial communication. I learned a lot about coding through Arduino and attempting to fix minor issues, so if you are looking to improve your skills, I would suggest giving this a shot!
Further Enhancements
- Visual Output: Connect LEDs to represent the binary values visually.
- Multiple Inputs: Expand the system to accept more button inputs and larger binary numbers.
- User Interface: Implement an LCD display to show the binary and decimal values without needing a computer.
- Yes or No Questions: Have the software display a yes or no question and experiment with different answer choices
This project not only enhances your understanding of basic electronics and programming but also lays the groundwork for more advanced projects involving inputs and digital systems. Best of luck!