Introduction
- For part four of my arduino tutorial we will be building a binary counter.
- Binary is a way of representing numbers using base powers of 2.
- each number in binary is represented by a series of 1 and 0.
- each 1 or 0 represents a power of two.
- starting at the rightmost digit, this is 2^0. the digits immediatly to the left is 2^1, then the next digit is 2^2, etc.
- to convert to base 10, simply add up all the powers of 2 for which there is a 1 in the binary number.
- for example, the binary number 0101 in base 10 is 5.
- the 1 at the right most digit repesents 2^0
- the digit to the left is 0, so it is skipped over.
- the digit to the left of the first 0 is a 1 which represents 2^2
- finally, the left most digit is a 0, so it is skipped.
- so the final number is 2^0 + 2^2 which equals 5
- for more information on binary numbers, please see wikipedia
Parts needed
- for this project, you will need the following parts:
- Arduino
- BreadBoard
- (four) LED
- (four) 220 ohm resistors
- (one) push button
- (one) 2k ohm resistor
- wires
Circuit
here is the circuit diagram for the Binary Counter
- things to note:
- each LED is connected to its own 220 ohm resistor to ground
- each LED is connected to its own seperate pin on the arduino
- the push button has a pull down resistor connected to ground.
here is the circuit i built for this project.
Code
here is the code for this project. the complete code can be found here
- the new parts of the code for this project are:
- counter++
- the ++ on the end of counter is a post fix increment
- this is simply a short hand way to update the value of counter by 1.
- if(counter & 0b01)
- this code is checking if the binary number for counter has a 1 in the 2^0 place. if so, it executes the code in brackets.
- & is the bitwise AND operator
- for information on bitwise AND, please check wikipedia
- 0b01
- 0b indicates a binary number. everything after the b is intepreted as a binary number
- in this case, 0b01 is binary 1
- 0b indicates a binary number. everything after the b is intepreted as a binary number
- delay(150)
- delay isnt new, but debouncing is.
- debouncing is a way to prevent the arduino from reading multiple button presses which happen in quick succession.
- it is quite easy to get multiple quick buton presses when you just intended to press it once. this is a characteristic of buttons and relays
- debouncing is a way to prevent the arduino from reading multiple button presses which happen in quick succession.
- delay isnt new, but debouncing is.
- counter++
Results, Conclusions, and Final Remarks
- If all went well, your circuit should count up with each button press.you should see the LEDs count up in binary.
- since we only have four LEDs the highest number we can count to is 15.
- when count reaches 16 and higher, we have a situation known as overflow.
going further
further things to do with this project.
- you can add more LEDs to count to an even higher number
- using what you've learned about if statments, can you make counter reset back to 0 after it reaches 16?
- try making it count backwards. start at 15 and go down to zero, decreasing counter by 1 with each button press.
- hint: you might want to replace the postfix increment ++ with the postfix decrement --
- once you've got it working correctly, try removing the delay(150). you will see first hand how debouncing improves the performance of this circuit.
- try having it count up/down only using code, try eliminating the button altogether.
thank you for checking out my tutorial. check back soon for the next part of this tutorial.
No comments:
Post a Comment