12. Implement a 4-bit binary counter

Let's first understand the 4-bit binary counter before implementing it.

  • A 4-bit binary counter can count up to 2^4=16 states, ranging from 0x00 to 0x0F. The table below shows the counts for the counter.
Counter Value in DecimalCounter Value in HexadecimalBinary Count of Counter
00X000000
10X010001
20X020010
30X030011
40X040100
50X050101
60X060110
70X070111
80X081000
90X091001
100x0A1010
110X0B1011
120X0C1100
130X0D1101
140X0E1110
150X0F1111

Let's connect hardware,

We need to use a proper resistor for each LED to limit the current flowing through them to 10mA.

Calculating the Resistor Value

To ensure a 10 mA current through the LED, we need to select an appropriate resistor based on the supply voltage.

Case 1: 5V Supply

Resistor-calculation
  • LED forward voltage (Vf) = 1.8V (from datasheet)
  • Voltage across resistor (VR) = Supply voltage – Vf = 5V – 1.8V = 3.2V
  • Resistor value (R) = VR / I = 3.2V / 10 mA = 320 Ω

Standard resistor values near 320 Ω: 330 Ω or 300 Ω (whichever is available).

Similarly, Case 2: 3.3V Supply

  • Voltage across resistor (VR) = 3.3V – 1.8V = 1.5V
  • Resistor value (R) = 1.5V / 10 mA = 150 Ω

Standard resistor value: 150 Ω.

Interfacing the push button switch:

To change the LED blinking pattern, use a push-button switch. When connecting the switch, ensure it correctly provides GPIO levels (LOW and HIGH). For reliable detection of voltage levels, use either a pull-up or pull-down resistor configuration.

  • Case 1: 5V supply
switch-pullup-pulldown-configuration
  • Case 2: 3.3V supply: The approach is the same as above; simply use a 3.3V supply instead of 5V where applicable.

However, most microcontrollers’ GPIO pins include an internal pull-up resistor, which can be used for interfacing the switch.
Therefore, it is recommended to enable the internal pull-up resistor in your microcontroller’s settings.

So, by selecting a proper resistor, LED, and push-button switch, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!