2pcs 74HC595 with 16ch led + potentiometer.

Hello. I have connected board like this:

  • potentiometer connected to PIN A0.

I have modified code from click

After modified:

int latchPin = 4;
int clockPin = 5;
int dataPin = 3;

int potPin = 0;
int potVal = 0;
byte leds = 0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

void loop() 
{


  updateShiftRegister();
  potVal = analogRead(potPin);
  if(potVal <= 63&& potVal > 5)
  {
    bitSet(leds,0);
    updateShiftRegister();  
  }
  
  else if(potVal > 63&& potVal <= 126)
  {
    bitSet(leds,1);
    updateShiftRegister();  
  }
  
  else if(potVal > 126&& potVal <= 189)
  {
    bitSet(leds,2);
    updateShiftRegister();  
  }
  
  else if(potVal > 189&& potVal <= 252)
  {
    bitSet(leds,3);
    updateShiftRegister();  
  }
  
  else if(potVal > 252&& potVal <= 315)
  {
    bitSet(leds,4);
    updateShiftRegister();  
  }
  
  else if(potVal > 315&& potVal <= 378)
  {
    bitSet(leds,5);
    updateShiftRegister();  
  }
  
  else if(potVal > 378&& potVal <= 441)
  {
    bitSet(leds,6);
    updateShiftRegister();  
  }
  
  else if(potVal > 441&& potVal <= 504)
  {
    bitSet(leds,7);
    updateShiftRegister();  
  }
  
  else if(potVal > 504&& potVal <= 567)
  {
    bitSet(leds,8);
    updateShiftRegister();  
  }
  
  else if(potVal > 567&& potVal <= 630)
  {
    bitSet(leds,9);
    updateShiftRegister();  
  }
  
  else if(potVal > 630&& potVal <= 693)
  {
    bitSet(leds,10);
    updateShiftRegister();  
  }

  else if(potVal > 693&& potVal <= 756)
  {
    bitSet(leds,11);
    updateShiftRegister();  
  }

  else if(potVal > 756&& potVal <= 819)
  {
    bitSet(leds,12);
    updateShiftRegister();  
  }
  
  else if(potVal > 819&& potVal <= 882)
  {
    bitSet(leds,13);
    updateShiftRegister();  
  }  

  else if(potVal > 882&& potVal <= 945)
  {
    bitSet(leds,14);
    updateShiftRegister();  
  }
  
  else if(potVal > 945&& potVal <= 1023)
  {
    bitSet(leds,15);
    updateShiftRegister();  
  }  

      
  if(potVal < 945)
  {
    bitClear(leds,15);
  }
  if(potVal < 882)
  {
    bitClear(leds,14);
  } 
  if(potVal < 819)
  {
    bitClear(leds,13);
  }
  if(potVal < 756)
  {
    bitClear(leds,12);
  }
  if(potVal < 693)
  {
    bitClear(leds,11);
  }
  if(potVal < 630)
  {
    bitClear(leds,10);
  }
  if(potVal < 567)
  {
    bitClear(leds,9);  
  }
  if(potVal < 504)
  {
    bitClear(leds,8);  
  }
  if(potVal < 441)
  {
    bitClear(leds,7);  
  }
  if(potVal < 378)
  {
    bitClear(leds,6);  
  }
  if(potVal < 315)
  {
    bitClear(leds,5);  
  }
  if(potVal < 252)
  {
    bitClear(leds,4);  
  }
  if(potVal < 189)
  {
    bitClear(leds,3);  
  }
  if(potVal < 126)
  {
    bitClear(leds,2);  
  }
  if(potVal < 63)
  {
    bitClear(leds,1);  
  }
  if(potVal <5)
  {
    bitClear(leds,0);
  }


}

void updateShiftRegister(){
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin,clockPin,MSBFIRST,leds);
  digitalWrite(latchPin,HIGH);  
}

and the problem is that I have 2 pcs of 74HC595 and I don't know how can I change the code to work properly. Now I can turn on 2 led at the same time. I can't control each led "one by one" > 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16.

Regards

You'll need a sixteen bit "leds" variable, won't you?

I have connected board like this:

Then immediately remove that 1uF capacitor. It could damage your Arduino. That is a crap page you copied the circuit from.

You need a 0.1uF ceramic capacitor between power and ground of each 595 chip.

Grumpy_Mike:
Then immediately remove that 1uF capacitor. It could damage your Arduino. That is a crap page you copied the circuit from.

Long standing error (one of the many) on THIS site.
https://www.arduino.cc/en/Tutorial/ShiftOut
Cap should co between VCC (red wire) and ground.
Leo..

*I have 4,7uF smd + 100nF smd between GND and VCC near 74HC595.

gavron04:
I have 4,7uF smd + 100nF smd between GND and VCC near 74HC595.

That's fine, but remove the cap between latch pin and ground.
It does not belong there.
Leo..

gavron04:
*I have 4,7uF smd + 100nF smd between GND and VCC near 74HC595.

Then why was that not on the schematic you posted?

You need a 0.1uF ceramic capacitor on every 74HC595.

byte leds = 0;

and

bitClear(leds,11);

Do not sit nicely together do they.

A byte has only 8 bits, numbered 0 to 7. Read again reply 1

If I change:

byte leds = 0 ;

to:

int leds = 0 ;

nothing changes

gavron04:
If I change:

byte leds = 0 ;

to:

int leds = 0 ;

nothing changes

...because you only shift out 8 bits.

Need help.. :wink:

Ok. Got it.

int latchPin = 4;
int clockPin = 5;
int dataPin = 3;


int potPin = 0;
int potValue = 0;
int shiftValue = 0;
//uint16_t leds = 0;


void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}



void loop() 
{
// read pot &  update outputs to 2 daisy chained shift registers
potValue = analogRead(A0);
if ( potValue >=0 && potValue <=63)
{
shiftValue = 0b0000000000000001;
}

if ( potValue >=63 && potValue <=126)
{
shiftValue = 0b0000000000000011;
}

if ( potValue >=126 && potValue <=189)
{
shiftValue = 0b0000000000000111;
}

if ( potValue >=189 && potValue <=252)
{
shiftValue = 0b0000000000001111;
}

if ( potValue >=252 && potValue <=315)
{
shiftValue = 0b0000000000011111;
}

if ( potValue >=315 && potValue <=378)
{
shiftValue = 0b0000000000111111;
}

if ( potValue >=378 && potValue <=441)
{
shiftValue = 0b0000000001111111;
}

if ( potValue >=441 && potValue <=504)
{
shiftValue = 0b0000000011111111;
}

if ( potValue >=504 && potValue <=567)
{
shiftValue = 0b0000000111111111;
}

if ( potValue >=567 && potValue <=630)
{
shiftValue = 0b0000001111111111;
}

if ( potValue >=630 && potValue <=693)
{
shiftValue = 0b0000011111111111;
}

if ( potValue >=693 && potValue <=756)
{
shiftValue = 0b0000111111111111;
}

if ( potValue >=756 && potValue <=819)
{
shiftValue = 0b0001111111111111;
}

if ( potValue >=819 && potValue <=882)
{
shiftValue = 0b0011111111111111;
}

if ( potValue >=882 && potValue <=945)
{
shiftValue = 0b0111111111111111;
}

if ( potValue >=945 && potValue <=1023)
{
shiftValue = 0b1111111111111111;
}

digitalWrite (latchPin, LOW);
shiftOut (dataPin, clockPin, MSBFIRST, highByte (shiftValue)); // upper 2 bits
shiftOut (dataPin, clockPin, MSBFIRST, lowByte (shiftValue)); // lower 8  bits
digitalWrite (latchPin, HIGH);

}

Only. How Can I turn off first led after rotate potentiometer example 100 then I have first and second led turned on. When I turn back, always first led is blinking.

Done.

Added

if ( potValue >=0 && potValue <=20)
{
shiftValue = 0b0000000000000000;
}

Hello.

I have question about controlling brightness this leds.

How can I add photoresitor to control brightness? I need to set max. brightness when it is sunny and about 5% when it is night.

I have connected 16 leds. About 8mA per one led. All 128mA.

Regards,

Mateusz

gavron04:
How can I add photoresitor to control brightness?

Connect the two OE (output enable) pins of the 595s to a PWM pin.
AnalogWrite 255 to that pin turns the LEDs off, and a lower value makes them brighter (0 = full brightness).

Read an LDR with an analogue input pin, and map it's values to the brightness levels you want.
Some experimenting required.
Leo..

Thanks for reply.

HW isn't problem. But I have problem with software. I need to change code to PWM? Not as I have now like shiftOut/digitalWrite?

I need to change code to PWM?

No you have your code exactly how you have it now. You add a PWM capable output pin connected to the OE pins of the two shift registers and control the overall brightness of all the LEDs by doing an analogWrite of some value to that pin.

Something likt this > Brightness Control | Arduino Lesson 4. Eight LEDs and a Shift Register | Adafruit Learning System

About ADC with LDR > https://forbot.pl/blog/wp-content/uploads/2015/05/ard_3_4-650x419.png

Something likt this > https://......

Yes.

About ADC with LDR >

Yes, but just the LDR part as the LED is not connected to a PWM capable pin so you can't dim it. The PWM capable pins have a ~ symbol next to them.