1

I am trying to use garage door safety sensors as a way of counting cars. From my understanding, if there is no object (car) between the sensors, then they output a 3.3V signal every 6 milliseconds. If an object (car) is detected, then they output a close to 0 voltage. Has anyone done this before? Currently, when I use digitalRead(), whether I compare it to LOW or HIGH, it results as true. I had a hard time finding a circuit schematic, but what I have seems to work.

Here is my circuit: enter image description here

SensorA and SensorB correspond to the two sensors of a garage door safety sensor.

Here is the output of the gpio tested using an oscilloscope, when no object is between the sensors: enter image description here

Here is my code:

#include <stdio.h>
#include <unistd.h>
#include <wiringPi.h>

#define IRSensor 21  //Pin 40

int main(int argc, char **argv) {

if(wiringPiSetupGpio() != 0){
    printf("ERROR\n");
    return 1;
}

pinMode(IRSensor, INPUT);   
printf("Checking\n");

 while(1){
    if(digitalRead(IRSensor == LOW)){
        printf("LOW\n");
        sleep(1);   
    }
    if(digitalRead(IRSensor == HIGH)){
        printf("HIGH\n");
        sleep(1);   
    }
 }

return 1;
}

Current Output: LOW HIGH LOW HIGH . . . . etc.

Expected Output:

LOW (when something is between the sensors)

HIGH (when nothing is between the sensors)

3
  • 1
    I can't see a question. Could you please edit the question and make it clear what you want to know?
    – joan
    Mar 25, 2018 at 20:22
  • @joan, you missed it ... it is Has anyone done this before?
    – jsotola
    Mar 26, 2018 at 5:49
  • "Has anyone done this before?" What??? - connect an unspecified sensor to the Pi? The circuit as drawn is obviously incomplete and will do nothing, as will the code.
    – Milliways
    Mar 26, 2018 at 5:56

1 Answer 1

1

A couple of points.

The digitalRead(x) function returns the current level of the GPIO numbered x. For instance digitalRead(21) will return the current level of whatever GPIO is assigned the number 21 by wiringPi. Your code contains a logical error.

digitalRead(IRSensor == LOW) will evaluate to false as 21 is taken to be true and LOW (0) is taken to be false. False is represented by zero in C so the call reads GPIO 0.

digitalRead(IRSensor == HIGH) will evaluate to true as both 21 and HIGH (1) are taken to be true. True is represented by one in C so the call reads GPIO 1.

The code should be changed to if(digitalRead(IRSensor) == LOW){ and if(digitalRead(IRSensor) == HIGH){.

I'd probably use an else clause instead of the double test.

The other point is that the signal is brief. You can not guarantee that you will be reading the pulse especially as the reads are only made once per second.

You need to explore the use of interrupts within wiringPi. Get wiringPi to tell you when there is a rising edge. If there has not been a rising edge for more than 6ms that means a car is present.

The following Python script shows the sort of logic you need to implement.

#!/usr/bin/env python

IR_Sensor=21

import time
import pigpio

def cbf(gpio, level, tick):
   global car_present
   if level == pigpio.TIMEOUT:
      if not car_present:
         car_present = True
         print("car present")
   else:
      if car_present:
         car_present = False
         print("no car present")

pi = pigpio.pi()
if not pi.connected:
   exit()

car_present = False

pi.set_mode(IR_Sensor, pigpio.INPUT)
pi.set_watchdog(IR_Sensor, 8) # 8 ms timeout on no activity
cb = pi.callback(IR_Sensor, pigpio.RISING_EDGE, cbf)

while True:
   time.sleep(1) # spin forever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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