Wiring 74HC595 Buttons + Teensy 4.1

Salchicha

New Member
I'm trying to get my 74hc595 working with Teensy 4.1, I couldn't find any examples of this so I got ChatGPT to write the code for me, I'm not sure if I've wired it up wrong or the code is wrong but it is not registering button presses, the serial.prints are showing all buttons as 0 even when I press one.




C++:
#include <SPI.h>

const int latchPin = 10;  // Connects to Pin 12 (Latch/ST_CP) on 74HC595
const int dataPin = 11;   // Connects to Pin 14 (Data/SI) on 74HC595
const int clockPin = 13;  // Connects to Pin 11 (Clock/SH_CP) on 74HC595

void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}

void loop() {
  // Read button states
  byte buttonsState = readButtons();

  // Print button states
  Serial.print("Button States: ");
  for (int i = 0; i < 8; ++i) {
    Serial.print(bitRead(buttonsState, i));
  }
  Serial.println();

  delay(500);  // Add a delay to avoid fast button state changes
}

byte readButtons() {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, B11111111);  // Shift in all 8 bits
  digitalWrite(latchPin, HIGH);

  return digitalRead(0);  // Replace 0 with the appropriate pin number where the Q0 is connected
}
 
Curious, why bother using the 595, there is enough IO on Teensy,
as show in your breadboard, to handle some buttons.

As an aside, 4 buttons + A/D would suffuce on 1 pin of I/O :



You of course have to worst case the R values, drift, tolerances, and A/D
accuracy, Vref, to insure you always get a good solid value for each button.


Regards, Dana.
 
Please check if you have figured Data, Latch, and Clock pins correctly. Check the pinout of Teensy 4.1. Data pin should be connected to MISO. I can see that pin 11 of Teensy 4.1 is MOSI. 12 is MISO.


If you want try to try other projects using 74HC595, you can try this sliding LED project.

 
Yes your right, I've changed it to pin 12 and I also had pin 13 on the teensy in 14 by mistake, I've corrected both of these but still not working. Can anyone confirm that my wiring of the buttons is correct?

danadak I've got an ST7735 display, a MIDI output, a little button microphone, a NeoTrellis and probably about 8 KY040 rotary encoders (which I'll use a MCP23017 port expander for) and 8 buttons. I want to try and fit the audio shield on as well (I dont need the audio shield SD card).
 
The 74HC595 should be used for OUTPUTS in that configuration - it has serial in, parallel out capability!
The shiftOut command is sending data to set it's outputs.

You could be driving LEDs, but not reading switches.


For inputs, use eg. a 74HC165. Wire that appropriately and use shiftIn() to read the data from it:
 
It would, but you are better off using SPI rather than I2C if you can - whether for shift registers or dedicated I/O expanders; SPI is far faster that I2C, and you must remember that the normal Arduino libraries use "blocking" functions for such interfaces.
(ie. The program stops until the I2C or SPI transaction function has completed).

Personally, I would not try to run encoders through I2C, you will likely either be losing data or not have time to process much else.

There are other parallel to serial shift registers - eg. 74HC166, CD4021.

Reading data from such as those using hardware SPI at 10MHz would only take around 1uS per byte, vs. about 12uS for one byte by I2C at 1.7MHz, the fastest the 23017 can operate at.

(The 74HC ICs can actually run at 25MHz or more).
 
Out of curiosity, how do you expect to read an output pin?
Code:
void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}
I too would just read a number of pins with the processor. How many push buttons do you need?

Mike.
 

I didn't think you could use shift registers with encoders as there are 3 pins to consider (SW, DT, CLK) (its a push button encoder).

I got confused, as did ChatGPT. I'm newb at this....ChatGPT should have know better though!
I need 8 buttons and 8 encoders.
 
I didn't think you could use shift registers with encoders as there are 3 pins to consider (SW, DT, CLK)
They are just remote inputs - you process them once they are stored in the MCU RAM variables, treating them the same as you would if reading an I/O port directly.

It would still be best if you could read the encoder AB signals directly on the MCU though, and use the external I/O for buttons and switches.

Or use a different MCU module - eg. an ST Nucleo F103RB, which is around £10 or less.
That has 44 internal I/O; there are other modules with even more.


Or this one costs a bit more but has over 100 i/o !!

Both can be programmed with the Arduino IDE, if you add the STM32Duino board set.

They are also fast, 32 bit CPUs with lots of memory.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…