2

I'm trying to intercept sensor data with the RPi3. I use Raspbian and a Python script. One sensor is captured accurately, the other one prints results from both sensors. The sensor cables are the RPi3 inputs. The wiring is as follows on picture 1. I used resistors as in picture 2.

enter image description here picture 1

enter image description here picture 2

enter image description here picture 3

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
from time import sleep

GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)

buttonstate = 0;
lastbuttonstate = 0;
buttonstate1 = 0;
lastbuttonstate1 = 0;


while True:
    buttonstate = GPIO.input(20)
    buttonstate1 = GPIO.input(21)

    if buttonstate !=lastbuttonstate:
        if buttonstate == 1:
            print("Sensor1 on")
        time.sleep(0.1)
    buttonstate = lastbuttonstate

    if buttonstate1 !=lastbuttonstate1:
        if buttonstate1 == 1:
            print("Sensor2 on")
        time.sleep(0.1)
    buttonstate1 = lastbuttonstate1

When triggering the sensors (e.g.): Triggering sensor 2 Sensor2 on Sensor2 on Sensor2 on Sensor2 on Sensor2 on result without break

Triggering sensor 1 Sensor1 on Sensor1 on result with break between (had to stop trigger the sensor and trigger it again)

sometimes when triggering sensor 1 Sensor1 on Sensor1 on Sensor2 on Sensor1 on Sensor1 on Sensor2 on Sensor2 on Sensor1 on Sensor1 on Sensor1 on randomly printing wrong sensor

When connecting the inputs with terminal ground and reading the input directly the signals won't be send to the terminal but to the RPi3.

Where is the mistake?

  • This may be a crosstalk issue. Try moving the GPIO pins you use in order to physically separate the wires. You could also move the wiring on your protoboard... as there may be crosstalk on the board... Protoboards are not great for signal isolation. – RubberStamp Nov 27 '17 at 15:47
  • 1
    You don't mention any timings of the sensors. When a sensor "triggers", how long is the voltage high? After a trigger, what is the minimum time before another trigger? (I'm deliberately not using square wave terminology, but feel free to use it if you understand it.) – Chad Farmer Nov 30 '17 at 20:44
2

The first thing I would do is correct the logic.

buttonstate = lastbuttonstate
...
buttonstate1 = lastbuttonstate1

should be

lastbuttonstate = buttonstate
...
lastbuttonstate1 = buttonstate1
  • This doesn't work. After I changed the logic, the state change isn't recognized anymore. – Steven Nov 30 '17 at 15:06
  • Without the correction proposed by joan, both lastbuttonstate variables are initialized to zero and then are never changed. If you change lastbuttonstate and lastbuttonstate1 to "0" your program will behave the same. The assignment to buttonstate and buttonstate1 is irrelevant because the while loop starts by assigning those variables. – Chad Farmer Nov 30 '17 at 20:04
  • I can't seem to enter formatted code as a comment... – Chad Farmer Nov 30 '17 at 20:45
0

The following proposed code (caveat: I'm not familiar with raspian)

  1. properly checks for an input state change
  2. doesn't assume the state change was 0 to 1
  3. properly implements the 'debounce' time
  4. properly updates the 'prior' state indicators
  5. outputs a message that indicates what actually happened

and now the proposed code:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
from time import sleep

GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)

buttonstate = 0;
lastbuttonstate = GPIO.input(20);  // initialize 'prior' state
buttonstate1 = 0;
lastbuttonstate1 = GPIO.input(21); // initialize 'prior' state


while True:
    buttonstate = GPIO.input(20)

    if buttonstate != lastbuttonstate:
        time.sleep(0.1)   // debounce time

    buttonstate = GPIO.input(20) 

    if buttonstate != lastbuttonstate:  // check input again
        print("Sensor1 changed state")
        lastbuttonstate = buttonstate

    buttonstate1 = GPIO.input(21)

    if buttonstate1 != lastbuttonstate1:
        time.sleep(0.1)  // debounce time

    buttonstate1 = GPIO.input(21)

    if buttonstate1 != lastbuttonstate1:  // check input again
        print("Sensor2 changed state")
        lastbuttonstate1 = buttonstate1

Note: in the picture of the peg board there seems to be a missing jumper between the two blocks for the signal grounds

0

I figured it out, how i can setup the RPi3 with my sensors to read five sensors in a loop without having interferences or floating.

This is my setup. Two sensors are connected to a single GND PIN. Between GND and the Input wire are 100k resistors. The other resistors are 10k resistors. enter image description here

The code shows that a triggered sensor changes it's variable to 1. At the end all sensor states are printed out. This repeats every 0.3 sec. E.G. Sensor 1 and 3 are triggered. console: 101000

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
from time import sleep

GPIO.setup(7, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(12, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(21, GPIO.IN)


buttonstate0 = 0;
buttonstate1 = 0;
buttonstate2 = 0;
buttonstate3 = 0;
buttonstate4 = 0;
buttonstate5 = 0;


while True:
    buttonstate0 = GPIO.input(7)
    buttonstate1 = GPIO.input(24)
    buttonstate2 = GPIO.input(12)
    buttonstate3 = GPIO.input(16)
    buttonstate4 = GPIO.input(23)
    buttonstate5 = GPIO.input(21)


    if buttonstate0 == 1:
        a=1
    else:
        a=0

    if buttonstate1 == 1:
        b=1
    else:
        b=0

    if buttonstate2 == 1:
        c=1
    else:
        c=0

    if buttonstate3 == 1:
        d=1
    else:
        d=0

    if buttonstate4 == 1:
        e=1
    else:
        e=0

    if buttonstate5 == 1:
        f=1
    else:
        f=0


    print(a,b,c,d,e,f)


    sleep(0.3)

negative: - If all sensors are on the program prints 000000. - All sensor states are printed over and over again wether the sensor states changes or not.

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.