Go Down

Topic: Need Help with LED Binary Counter (Read 5318 times) previous topic - next topic



If you want, I did a code for a binary counter. It count from 0 to 15, in binary. I did this code in July 2011.  I did not learn about bitRead at the time of the code was written.

Just change this :

Code: [Select]

const int led[4] = {9,10,11,12}; // 9 to 12 are the digital pins
// That is an array. Pin 9 is box number 0. pin 12 is box 3.
// An array is like an hotel  with room numbers. The box is the room number, and the inside of the room is the pin.    

And replace with your pins. Don't worry about the transistors in the comment at this time. I did use a transistor to switch the Leds. But for now, just use a limiting resistor and a Led connect to a digital pin at this time.  

Code: [Select]


Version 1.0
Size : 1182 Bytes

This program will count 0 to 15 and display the result in binary.


The hardware is connected to a transistor to use as a switch.

Need : 4 Transistor : Common NPN ex: 2N3904
      4 1 K resistor : between the digital pin and the base of the transistor.
      4 LED : Any color
      4 330 ohms resistor : current limiting for the LED.
And try to power the LEDs circuit seperated. And don't forget to connect the gnd of the
circuit with the Arduino GND.

Created by Serge Desjardins  --->  Techone
          Toronto, ON   CANADA

Idea from "Bit Math Tutorial by CosineKitty" in Arduino Playground.

const int led[4] = {9,10,11,12};

/* Set Output LEDs  --->  A B C D   MSD --- LSD
         Digital pin    12 11 10 9

  Common variables

int pin;
int counting;

void setup()

 Set the pin 12, 11 , 10 , 9  to OUTPUT
  for (pin =0; pin < 4; pin++ )
    pinMode (led[pin], OUTPUT);

void loop()

 Counting routine from 0 to 15 and wait for 1 second
for ( counting = 0; counting < 16; counting++)
   delay (1000); // The One second delay

void LEDview()

 The LED display subroutine. The trick is to brake down the 4 bit data
 ( 0 to 15 ---> 0000 to 1111, so I need HIGH or LOW ( 1 or 0 ) Humm ... boolean
isolate the proper bit and shift the bit, so it equal in value 1 or 0,
so I use the AND operator & ( to isolate ) and the SHIFT RIGHT >> ( to move at
the proper spot )

My idea came from "Bit Math Tutorial by CosineKitty" in the Arduino playground

From there, you can use a 7447 to count the data ( change 16 to 10 in the counter line ) or
other use if need parallel data, the only limitation is the digital lines do not come out at the same
time, but in this way, it is almost at the same time. Bear in mind the small delay after each digitalWrite

 digitalWrite (led[3], boolean ((counting & 8)>>3));
 digitalWrite (led[2], boolean ((counting & 4)>>2));
 digitalWrite (led[1], boolean ((counting & 2)>>1));
 digitalWrite (led[0], boolean (counting & 1));

Jack Christensen

I think what we have discovered here may be sort of a high-tech version of those birthday candles that can't be blown out. Of course this could be a much more cruel trick to play on someone ]:)

They also make LEDs that flicker like a candle flame. I have a few, pretty cool.



I have one of those in my leds parts bin. I place one of them and I see it "blink" "blink".... I replace that led and things work fine.

And I always do a test code to see my hardware setup work. Even I do without Arduino before connecting to see if things work fine.


May 20, 2016, 11:05 pm Last Edit: May 20, 2016, 11:37 pm by umurri
I'm sorry but I don't understand why for a binary counter nobody use a simple sketch like this one, following.
Doing so, you will have the 6 lower bits on B0...B5 and the higher two bits on D6...D7 (even if I haven't yet discover why)

String ProgramName = "1505-CounterOnPort";    // write here the filename
byte Counter;        // definition of the sole variable

void setup() {                
//--------------- RX-TX LED's ---------------------------
Serial.begin(9600); //This pipes to the serial monitor
Serial.begin(9600); //This is the UART, pipes to sensors attached to board

// initialize the PORTs to use

DDRB = B11111111;      
DDRD = B11111111;      

void loop() {
//  --------------- Counter ---------------------------------


//  --------------- Outputs ---------------------------------

PORTD = Counter;        // Lower order 6 bits
PORTB = Counter;        // Higher order 2 bit

Go Up