I moved the whole initial post to avoid misunderstanding and confusing everyone, sorry

after all the reading i came up with this:

this is my code for ultrasonic sensor that going into digital pins in my arduino:

int trigPin1=2;
int echoPin1=3;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);

void loop() {
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(15); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = duration1/58;

  Serial.print (distance1);
  Serial.print (",");

and this is the idea i wrote to set the "extra digital outputs" on the 595 chip:

int DS_pin = 8;
int STCP_pin = 9;
int SHCP_pin = 10;

void setup() 
{ // put your setup code here, to run once:
  pinMode(DS_pin, OUTPUT);
  pinMode(STCP_pin, OUTPUT);
  pinMode(SHCP_pin, OUTPUT);

bool registers[8];

void writereg()
  digitalWrite(SHCP_pin, LOW);

  for (int i = 0; i<=7; i++)
    digitalWrite(STCP_pin, LOW);
    digitalWrite(STCP_pin, HIGH);
    digitalWrite(SHCP_pin, HIGH);

void loop() 
{// put your main code here, to run repeatedly:


now if you guys have a little time to check it, and tell me what should i wrote so the ultrasonic sensor will be working with the 595 chip's outputs?

Let say im going to put the Trigger pin into Q0 and Echo pin into Q1

thanks in advance

  • Can you clarify what you mean by "the extra slot"? What extra slot? – Nick Gammon Dec 18 '16 at 5:07
  • by extra slot i mean extra digital input, because im running out of digital input on the actual board, so i've been searching and found out that you can pull "extra" digital input with multiplexing it using IC - HC74595 – Mahardika Dec 18 '16 at 5:25
  • You mean outputs, right? As in "extra digital output" as the 74HC595 has 8 digital outputs. – st2000 Dec 18 '16 at 6:09
  • Yes sir, output, my bad – Mahardika Dec 18 '16 at 6:48
  • i want to use the extra slot from HC74595 - what extra slot on the HC74595? im running out of digital input on the actual board - what "actual board"? Can you post a schematic? – Nick Gammon Dec 18 '16 at 6:51

It is good that you are thinking of abstraction in order to simplify programming. Simplifying programming by using familiar names makes it easier to understand the code.

In your example:

int triggerPin=2
pinMode(triggerPin, OUTPUT)

..you are abstracting the actual pin used to trigger the ultrasound by calling it by a familiar name "triggerPin". You could have just as easily wrote the same code as:

pinMode(2, OUTPUT)

Now you are asking how to abstract the pins when connecting the Arduino to the 74HC595. The truth is, you don't have to. But to make it easier to write & read the code you can use what ever names make sense to you. In this example of connecting an Arduino to a 74HC595 there are 3 Arduino pins connected to the 74HC595. They called them "dataPin", "latchPin" and "clockPin". If these make sense to you use these names.

Add: can i address the output like this?

 #define LED_PIN     Q1

No, nothing knows what "Q1" is. Remember, you are adding this hardware to the Arduino. So nothing, not the compiler nor a library, knows about the hardware you added let alone a pin called Q1 on the added hardware.

First you need to understand how to set Q1 through Q8. Refer to the 74HC595 Arduino tutorial linked to above. Inspect the example programs. Once you understand how to set / un-set Q1 through Q8, you can decide if you what to simply shift out a number or if you want to use abstraction giving names to the shift register output pins.

If I were only writing patterns, for example lighting up 8 LEDs, I would stick to numbers. However, if I were controlling different things, for example a light, a motor & a buzzer, I would assign each a unique name and build my program around the names.

  • @Mahardika, if this is what you were asking about great. If you want to go further and ask about the output pins of the 74HC595, re-word or add-to your question to make it clear what you want. – st2000 Dec 18 '16 at 6:15
  • this is all making more sense to me, and for others who viewed this question haha.. thank you! – Mahardika Dec 18 '16 at 6:37
  • This all makes a lot of sense. But I don't see where "extra slot" comes into it. – Nick Gammon Dec 18 '16 at 6:53
  • Yes, I think Mahardika meant to say "extra output". It did occur to me to edit the Question to make things clearer. – st2000 Dec 18 '16 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.