Object counter by an infra red sensor on Raspberry with Python

Hello friends I made an object counter using an infrared sensor and Python, but I would like help to refine the code.

Because this my counter has a problem, if the object stops in front of the sensor the counter is counting infinitely, my goal is that this my counter would only do the count when the sensor sends the signal that it has been activated and deactivated, making it really only do the count when a new object passes in front of the sensor.

I did this programming in C++ on Arduino and it is very simple, but in Python I am having difficulties.

I appreciate your attention.

import RPi.GPIO as gpio

gpio.setmode(gpio.BOARD)

gpio.setup(36, gpio.IN)


Objetos=0

while True:

    sensor = gpio.input(36)
    if sensor ==  0:      
        Objetos+=1
        print("Objetos Detectados", Objetos)
Author: Woss, 2019-01-17

1 answers

Whereas sensor stay as 1 when there is no object in front of the sensor, you can make a loop that waits for the object to pass; something like:

from functools import partial

sensor_ir = partial(gpio.input, 36)

while True:
    if sensor_ir() == 0:
        objetos += 1
        # Enquanto o objeto não sair da frente...
        while sensor_ir() != 1:
            pass

Thus enters if when it detects an object and only exits the second while when the object passes through completely. I also used the function functools.partial to make the code more readable, making sensor_ir() equivalent to gpio.input(36).

But I need to warn that this solution does not provide for bouncing in sigal. That is, if the its sensor vary between 0 and 1 for short intervals (noises) due to the internal characteristics of it the system may still count wrong.

If you do not want to hold your application in an infinite loop, you can create a flag that indicates that the current object has been accounted for or not.

contabilizado = False

while True:
    if sensor_ir() == 0 and not contabilizado:
        objetos += 1
        contabilizado = True
    elif sensor_ir() == 1:
        contabilizado = False

Thus, it will only be added When the sensor is 0 and contabilizado is false; immediately contabilizado becomes true, so in the next iterations in which sensor is still 0 the object will not be counted again. Only from the moment sensor becomes 1, which is when the object leaves the sensor, contabilizado returns to false allowing to count the next object.


About bouncing, it is natural for a sensor to have noises between transitions. For example, the graph below represents the behavior of the signal when pressing a button. It should be set to 0 when the button is free and 1 when pressed; however, when pressed, there will be a transition noise in which the signal will vary between 0 and 1 in an "uncontrolled"way. For a faithful acquisition of the signal it is necessary to treat this behavior in order to ensure that the reading is not made in this period.

insert the description of the image here

 2
Author: Woss, 2019-01-17 16:41:19