Page 1 of 1

74HC595 Shift Register

Posted: Tue Dec 11, 2018 3:48 am
by Greeniverse
Hi All,

I am currently trying to control a TI 74HC595 Shift register using my ESP32 DevKit C. I think my problem seems to do with the data stream or clock rate. I try and turn a single output on and off in a loop and the high bit takes up 3 outputs and rather than turn off, it shifts the bit down a few pins.

Code: Select all

#include <Wire.h>
#include <MultiShiftRegister.h>

const int latchPin     = 12;                                //ESP32 Pin 12
const int clockPin     = 14;                                //ESP32 Pin 14
const int dataPin      = 27;                                //ESP32 Pin 27

MultiShiftRegister msr (1, latchPin, clockPin, dataPin);

void setup() {
  Serial.begin(9600);

  pinMode(latchPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(clockPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(dataPin, OUTPUT);                                 //Setting Pin Orientation
  delay(500);
}
void loop() {
  msr.set_shift(1);
  delay(2000);
  msr.clear_shift(1);
  delay(2000);
So in goes: LEDs 1-3 are on for 2 seconds, then LEDs 3-5 are on, then LEDs 5-7 are on. This code worked fine on an Arduino Uno. Any help would be appreciated! shift register library is included.

Re: 74HC595 Shift Register

Posted: Tue Dec 11, 2018 7:43 am
by ESP_Sprite
Moved to the Arduino subforum.

Re: 74HC595 Shift Register

Posted: Tue Dec 11, 2018 4:12 pm
by idahowalker
Perhaps you may reconsider the code you are using from the Uno in relation to using an ESP32. For one look at the clock rate of the ESP32, Do you want your dual core ESP32 sleeping for a whole 2 seconds when it can be doing other things?
Any ways,

On the ESP32 you have 4 timers available for use, timers 0-3.

Code: Select all

#include <Wire.h>
#include <MultiShiftRegister.h>
////
#define latchPin     = 12                                //ESP32 Pin 12
#define clockPin     = 14                                //ESP32 Pin 14
#define dataPin      = 27                                //ESP32 Pin 27
#define TIMER_FOUR 3
#define FourK 4000
#define TwoK 2000
////
volatile int iTicCount = 0; //counts milliseconds
////
MultiShiftRegister msr (1, latchPin, clockPin, dataPin);
////
void IRAM_ATTR onTimer()
{
  iTicCount++;
  //
  if ( iTicCount == TwoK )
  {
msr.set_shift(1);
  }
  if ( iTicCount == FourK )
  {
    msr.set_shift(1);
    iTicCount = 0;
  }
  //
}
void setup()
{
//
 /* Use 4th timer of 4.
    1 tick 1/(80MHZ/80) = 1us set divider 80 and count up.
    Attach onTimer function to timer
    Set alarm to call timer ISR, every 10000uS and repeat / reset ISR (true) after each alarm
    Start an timer alarm
  */
  timer = timerBegin( TIMER_FOUR, TimerDivider, true );
  timerAttachInterrupt( timer, &onTimer, true );
  timerAlarmWrite(timer, OneK, true);
  timerAlarmEnable(timer);
  ////
   pinMode(latchPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(clockPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(dataPin, OUTPUT);                                 //Setting Pin Orientation
  //
  }
  void loop() {} 
 
As a possibility.

Re: 74HC595 Shift Register

Posted: Sat Dec 15, 2018 8:27 am
by Greeniverse
Hi idohowalker,

Thanks for the reply.

I think I may have worded my problem incorrectly. The looped set_shift and clear_shift turns a pin on the shift register high and low to blink an LED. It's just an example to visually test the shift registers.

My main problem is that when I try and do this, the shift register is only supposed to turn on 1 LED by sending a byte (ie sending B01000000 should turn on the 2nd LED from the left). After i send the byte it turns on 3 LEDs. I think it has something to do with the length of a an up tic is longer than a single clock pulse and so it registers B01110000.

This is all speculation and I don't really know how to change any of the hardware stuff. I didn't do any computer science stuff at all so I don't really know how the clock rates between the shift register and ESP32 relate. If i'm close, is there anything that could help?

Thanks