1

I have setup a basic circuit to control a relay using a Raspberry Pi 4B. I have tried controlling it with both a push button and the Bluedot app. When implementing with the push button, the GPIO pins output 3.25V and the circuit works perfectly. However, when using Bluedot and changing nothing except for the main loop in the python script, the GPIO pins suddenly output 1.8V. Not enough to activate the transistor to the relay unless I drop the resistor value from 22k to 4.7k. I need to implement other hardware though and cannot rely on the pins outputting 1.8V. If I run both scripts simultaneously, the voltage is 2.6V with the button and somehow -1.6V with bluedot.

The code when using the button:

#Import GPIO and time library
import RPi.GPIO as GPIO
import time


#Set mode for pin numbering
GPIO.setmode(GPIO.BOARD)

#Define pins and configure data direction
button = 36
relay = 16

def setup():
   GPIO.setup(relay, GPIO.OUT)
   GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)
   

#Monitor button status. Relay goes high for 4 seconds when button is pressed
def loop():
   while True:
      if GPIO.input(button) == GPIO.HIGH:
       print("Door Open")
       GPIO.output(relay, GPIO.HIGH)
       time.sleep(4)
      else:
       print("Door closed")
       GPIO.output(relay, GPIO.LOW)
       time.sleep(0.1)
       
def destroy():
   GPIO.cleanup()
   
def main():
    setup()
    try:
        loop()
    except KeyboardInterrupt: # press Ctrl + C to exit script
        destroy()
        
if __name__ == '__main__':
    main()

The code when using Bluedot:

import RPi.GPIO as GPIO
import time
from bluedot import BlueDot

#Import BlueDot
bd = BlueDot()

GPIO.setmode(GPIO.BOARD)

relay = 16
led = 12

def setup():
    GPIO.setup(relay, GPIO.OUT)
    GPIO.setup(led, GPIO.OUT)

#Relay is active while button is held down
def loop():
  while True:
    bd.wait_for_press()
    GPIO.output(relay, GPIO.HIGH)
    GPIO.output(led, GPIO.HIGH)
    print("blue button pressed")
    time.sleep(0.1)
    
    bd.wait_for_release()
    GPIO.output(relay, GPIO.LOW)
    GPIO.output(led, GPIO.LOW)
    print("blue button released")
    time.sleep(0.1)
    
    
def destroy():
    GPIO.cleanup()
    
def main():
    setup()
    try:
        loop()
    except KeyboardInterrupt: # press Ctrl + C to exit script
        destroy()
        
if __name__ == '__main__':
    main()

The circuit diagram with the resistor (R_B=((V_Pi-0.6) h_FE)/I_RC):

schematic

simulate this circuit – Schematic created using CircuitLab

Thanks.

New contributor
Nathan De Meyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

1

You state "the GPIO pins suddenly output 1.8V" - this is IMPOSSIBLE (unless the GPIO is grossly overloaded). For a lightly loaded circuit the GPIO output will be >2.5V or <0.8V.

It is unclear what this unusual "code when using Bluedot:" is actually doing but it is most likely it is switching ON & OFF giving an average voltage.

"The code when using the button:" is polling for input, which in any language, is a poor solution.

I would suggest you try a callback e.g. gpiozero Run a function every time the button is pressed:

from gpiozero import Button
from signal import pause

def say_hello():
    print("Hello!")

button = Button(2)

button.when_pressed = say_hello

pause()

You should implement something similar for Bluedot, although you would need to investigate what the library actually does or supports.

PS using a 22kΩ resistor is unrealistic - this will give a current of 150µA which is unlikely to saturate the transistor. The highest resistance I would use is 3.3kΩ

Your Answer

Nathan De Meyer is a new contributor. Be nice, and check out our Code of Conduct.

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.