Main Content

Circuitos_1

Today we will build a circuit to read multiple inputs (buttons or others) using an IC that converts a parallel input into a _output series.

The Circuit uses a 74HC165 IC to read the inputs. Pull-up resistors are used in all inputs to ensure a steady state when the button is not depressed.

This circuit can be extended to more inputs using more IC in cascade. Pin 1 (/PL) and 2 (CP) are connected in parallel on all 74HC165. Pin 10 (DS) of the first IC should be connected to pin 9 (Q7) of the second and thereafter.
Pin 15 (/CE) has been connected to GND which means that the IC is always enabled.
Pin 10 (/Q7) is the complement of pin 9 (Q7).

It is required to ensure that all pins P0-P7 have a known state. All pins that are not used must be connected to the GND or to the 5V (via a pull_up).

Schematic

Circuitos_3_Schematics

Notice:

The circuits only have the relevant part for the IC 74HC165. The connection to the microcontroller is set in the sketch.
The circuit for the CSEduino can be checked in the page. It can also be used with an Arduino Uno.

Bill of materials (BOM)

Circuit:

  • 1x IC 74HC165 (U1)
  • 8x 10K Ohms Resistors (R1-R8)
  • 8x SPST Buttons (SW1-8)

IC/Component Pin-out

Circuitos_4_Pinout

Code

Two sketchs were made to use this circuit.

/*

  Pinos usados:
  D9 - Load Pin do 74HC165
  D12 - Q7 Pin do 74HC165
  D13 - CLOCK Pin do 74HC165

  Sketch que usa a comunicação SPI.

*/

#include <SPI.h>

const byte LATCH = 9;

void setup() {
  SPI.begin ();
  Serial.begin(9600);
  pinMode (LATCH, OUTPUT);
  digitalWrite (LATCH, HIGH);
}

void loop() {
  digitalWrite (LATCH, LOW);
  digitalWrite (LATCH, HIGH);
  byte optionSwitch = SPI.transfer(0);

  Serial.print("12345678 : ");
  for (byte i=0; i<8; i++) {
    Serial.print((optionSwitch & (1<<i)) != 0 ? 1 : 0);
  }
  Serial.println();

  delay(100);
}
// Sketch uses 2,892 bytes (8%) of program storage space. Maximum is 32,256 bytes.
// Global variables use 213 bytes (10%) of dynamic memory, leaving 1,835 bytes for local variables. Maximum is 2,048 bytes.

Other sketch that uses shiftIn and can use other pins:

/*

  Pinos usados:
  D9 - Load Pin do 74HC165
  D12 - Q7 Pin do 74HC165
  D13 - CLOCK Pin do 74HC165

  Sketch que usa o shiftIn.

*/

const byte LATCH = 9;
const byte DATA = 12;
const byte CLOCK = 13;

void setup() {
  Serial.begin(9600);
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, INPUT);

  digitalWrite(CLOCK, HIGH);
  digitalWrite(LATCH, HIGH);
}

void loop() {
  digitalWrite(CLOCK, HIGH);
  digitalWrite(LATCH, LOW);
  delayMicroseconds (5);
  digitalWrite(LATCH, HIGH);
  delayMicroseconds (5);

  byte optionSwitch = shiftIn(DATA, CLOCK, MSBFIRST);

  Serial.print("12345678 : ");
  for (byte i=0; i<8; i++) {
    Serial.print((optionSwitch & (1<<i)) != 0 ? 1 : 0);
  }
  Serial.println();

  delay(100);
}
// Sketch uses 3,048 bytes (9%) of program storage space. Maximum is 32,256 bytes.
// Global variables use 212 bytes (10%) of dynamic memory, leaving 1,836 bytes for local variables. Maximum is 2,048 bytes.