4 shift registers in and out getting artifact

So I'm using nano to translate from 4 shifts into 4 shifts out. simple enough.

But my shift out I get 6 of the last 8 channels are partially on? They are not displaying in the console but they are on non the less. If I add a pause between the read/send I can affect the 6 that are displaying to have their pulse.

Thanks, upfront for the help.

11-11-21.ino (3.2 KB)

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

(xFirst - 255) * -1)

It looks like you are trying to invert the bits. You should probably use the bit inversion operator:
~xFirst

And since these are 8-bit registers, you should probably use 'byte' values rather than 'int'.

1 Like

You need to define your statement: "channels are partially on" and how did you determine that. Without a schematic, not a frizzy picture it is hard to follow your project. The first thing you need to do is be sure all ground and power connections are good and shown on the schematic you plan on posting.

You can simplify your code quite a bit

//Pin callouts
//74HC165
const int load = 3;                // Shift in PL pin 1 GW
const int clockEnablePin = 4;      // Shift in CE pin 15 BW
const int dataIn = 5;              // Shift in Q7 pin 7 GR
const int clockIn = 6;             // Shift in CP pin 2 BL
//74HC595
const int latchPin = 7;            // Shift Out ST_CP pin 12 BL
const int clockPin = 8;            // Shift Out SH-CP pin 11 BW
const int dataPin = 9;             // Shift Out ser   pin 14 GW

const int numOfRegisters = 4;
byte registerState[numOfRegisters];

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

  // Setup 74HC165 connections
  pinMode(load, OUTPUT);
  pinMode(clockEnablePin, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);
  // Setup 74HC595 connecttions
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  regclear();
}

void loop() {
  GetInputs();
  delay(800);
  regWriteNew();
}

void regWriteNew() {
  digitalWrite(latchPin, LOW);
  for ( int i = 0; i < numOfRegisters; ++i ) {
    shiftOut(dataPin, clockPin, MSBFIRST, registerState[i]);
  }
  digitalWrite(latchPin, HIGH);
}

void regclear() {
  for (int i = 0; i < numOfRegisters; i++) {
    registerState[i] = 0;
  }
  regWriteNew();
}


void GetInputs() {
  //  serialPrintLoop("GetInputs");
  digitalWrite(load, LOW);
  delayMicroseconds(10);
  digitalWrite(load, HIGH);
  delayMicroseconds(10);

  Serial.print("Pin States:");
  // Get data from 74HC165
  digitalWrite(clockIn, HIGH);
  digitalWrite(clockEnablePin, LOW);
  for (int i = 0; i < numOfRegisters; ++i) {
    byte data = shiftIn(dataIn, clockIn, LSBFIRST);
    data = ~data;   // invert
    registerState[i] = data;
    Serial.println(data, BIN);
  }
  digitalWrite(clockEnablePin, HIGH);
}
1 Like

Thank you everyone for your input so far. Here are the images of the circuit layouts of what I'm working on.

30 Hall Sensors at one end of a cat 5, 2 switches, all put into 74HC165 (32channels) across a cat 5 (50 feet) to the receiver Arduino nano. The nano is translated then pushes the signal to 4 74HC595 to display the current status of the hall sensors and switches. I've also shown my version of the code here (properly, sorry learning). though I plan to update the code presented by @blh64.

The problem (with my code) is that the last 595 6 channels pulse very rapidly (appears on) with the LED, I've added the delay in the read/write to slow this pulse but would prefer without delay. I'm unsure where the issue is, either with my circuit design (also just learning) or the Arduino.


//Pin callouts
//74HC165
const int load = 3;                // Shift in PL pin 1 GW
const int clockEnablePin = 4;      // Shift in CE pin 15 BW
const int dataIn = 5;              // Shift in Q7 pin 7 GR
const int clockIn = 6;             // Shift in CP pin 2 BL
//74HC595
const int latchPin = 7;            // Shift Out ST_CP pin 12 BL
const int clockPin = 8;            // Shift Out SH-CP pin 11 BW
const int dataPin = 9;             // Shift Out ser   pin 14 GW 

int xFirst;                   // Holds first register output
int xSecond;                  // Holds second register output
int xThird;                   //
int xFourth;                  //
int xTouch;
int numOfRegisters = 4;
byte* registerState;
int xstart=0;

void setup() {
  // put your setup code here, to run once:
  registerState = new byte[numOfRegisters];
  for (size_t i = 0; i < numOfRegisters; i++) {
    registerState[i] = 0;
  }
  // Setup ////Serial Monitor
  //Serial.begin(115200);
 
  // Setup 74HC165 connections
  pinMode(load, OUTPUT);
  pinMode(clockEnablePin, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);
  // Setup 74HC595 connecttions
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
if (xstart==0){
  regclear();
  xstart=1;
}
GetInputs();
delay(800);
regWriteNew(); 
}

void regWriteNew(){
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, ((xFirst-255)*-1)); 
  shiftOut(dataPin, clockPin, MSBFIRST, ((xSecond-255)*-1));
  shiftOut(dataPin, clockPin, MSBFIRST, ((xThird-255)*-1));
  shiftOut(dataPin, clockPin, MSBFIRST, ((xFourth-255)*-1)); 
  digitalWrite(latchPin, HIGH);
  
}
void regWrite(int pin, bool state){
  //Determines register
  int reg = pin / 8;
  //Determines pin for actual register
  int actualPin = pin - (8 * reg);
  //Begin session
  digitalWrite(latchPin, LOW);
  for (int i = 0; i < numOfRegisters; i++){
    //Get actual states for register
    byte* states = &registerState[i];
    //Update state
    if (i == reg){
      bitWrite(*states, actualPin, state);
    }
    //Write
    shiftOut(dataPin, clockPin, MSBFIRST, *states);
  }
  //End session
  digitalWrite(latchPin, HIGH);
}                 

void regclear(){
  for (int i = 0; i < 32; i++) {  //CLEAR REGISTRY
    regWrite(i,LOW);
  }
}
void GetInputs(){
//  serialPrintLoop("GetInputs");
  digitalWrite(load, LOW);
  delayMicroseconds(10);
  digitalWrite(load, HIGH);
  delayMicroseconds(10);
 
  // Get data from 74HC165
  digitalWrite(clockIn, HIGH);
  digitalWrite(clockEnablePin, LOW);
  xFirst = shiftIn(dataIn, clockIn, LSBFIRST);
  xSecond = shiftIn(dataIn, clockIn, LSBFIRST);
  xThird = shiftIn(dataIn, clockIn, LSBFIRST);
  xFourth = shiftIn(dataIn, clockIn, LSBFIRST);
  digitalWrite(clockEnablePin, HIGH);
 
  // Print to monitor
  Serial.print("Pin States:");
  Serial.println((xFirst-255)*-1);
  Serial.println((xSecond-255)*-1);
  Serial.println((xThird-255)*-1);
  Serial.println((xFourth-255)*-1);

The picture is not a substitute for the schematic, it is hard to follow, however I do see a problem where you are powering and controlling via the CAT5 cable. Digital devices do not like long wires, they ring and can do all sorts of nasty things to your parts.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.