1

The following code is running on PI3 and is working perfectly fine. I try to provide a short description of what the code does. In this code I first set the BCM 12 as the pin of interest for my sensor which is a proximity sensor. Normally the pin is 1 unless I put something close to sensor in which case pin becomes 0. I pasted main parts of the code for your reference down here (I did not paste the whole code here). Now question is when I use the same code in PI4, it acts differently. Even without anything close to the proximity sensor, the pin is 0. In fact the pin always stays at zero. Expected behavior is: pin BCM 12 should stay 1 unless I put something next to sensor and in that case it should change into 0. But on PI4, it always stays at 0.

import RPi.GPIO as GPIO  # pylint: disable=import-error

PROXIMITY_SENSOR_GPIO_CHANNEL = 12
LED_GPIO_CHANNEL = 20


GPIO.setmode(GPIO.BCM)
GPIO.setup(PROXIMITY_SENSOR_GPIO_CHANNEL, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED_GPIO_CHANNEL, GPIO.OUT)


# this function checks whether the pin is triggered
def is_sample_present():
    return GPIO.input(PROXIMITY_SENSOR_GPIO_CHANNEL) == 0

def setup_proximity_callback():
    GPIO.add_event_detect(
        PROXIMITY_SENSOR_GPIO_CHANNEL,
        GPIO.RISING,
        proximity_sensor_callback,
        bouncetime=1
    )
  • 2
    Look at the graphic, its the pinout for Raspberry Pis. It has a marking (the big boxed 4) for things that only apply on the Pi 4. If you look at pin 12, it shows that it's GPIO 18, but also next to a big boxed 4 that it's SPI6 CEO IN (you can learn about SPI and I2C here). My guess is that SPI might need to be disabled for the pin to act as GPIO. But SPI is disabled by default, so unless you've enabled it, this isn't the solution. – Patrick Cook Sep 7 at 20:42
  • 2
    You can check if SPI is enabled with sudo raspi-config, navigating to Advanced Options->SPI. – Patrick Cook Sep 7 at 20:44
  • 2
    "The code crashes"... An error message would be helpful. – Patrick Cook Sep 7 at 22:13
  • 2
    Instead of running your entire script, which is clearly more complicated than just reading high/low from a pin, try writing a minimum working example of just reading high/low from a pin and running that across the two Pis. That would be miles easier to debug. – Patrick Cook Sep 8 at 2:03
  • 2
    My guess as to what is causing the difference between the two Pis is user error. Something must be configured differently between them. Make sure you have all the necessary dependencies installed properly, your physical wiring is correct, etc. Sometimes errors that look so complicated come down to something as simple as a missing comma. – Patrick Cook Sep 8 at 2:30
0

sudo apt-get update and sudo apt-get upgrade solved the problem.

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.