0

I have a problem with my Raspbeery Pi, which is connected to a relay box via GPIO pin 7. I have tested with Gpio.board and with gpio.bcm without success in this code:

> #This program connects with gpio7
> #setup pins
> #GPIO.setmode(GPIO.BOARD) GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.OUT)
> #GPIO.setup(7, GPIO.OUT)#mode Board while True:
>     GPIO.output(4, GPIO.HIGH)
>     #GPIO.output(7, GPIO.HIGH)#mode Board
>     time.sleep(5)
>     GPIO.output(4, GPIO.LOW)
>     #GPIO.output(7, GPIO.LOW) #mode Board
>     GPIO.cleanup()  #devuelve los pines a su estado inicial
> 
> exit()

This is the error both in one way and the other:

Traceback (most recent call last):File"/home/pi/Desktop/RelayPrograms/5_7OnSleepOff.py", line 13, in GPIO.output(4, GPIO.HIGH)RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

Am I missing something?

  • 2
    Try adding just this GPIO.setmode(GPIO.BCM) above this line GPIO.output(4, GPIO.HIGH) also you are using indentation so I am assuming this is part of a loop. Assuming that your code is not much longer than what is shown here, you should include the full code, or at least the entire loop. If this is part of the loop place add the line I mentioned before the loop. Remember you can basically copy blink an led code, it is functionally the same as toggling a relay. – Steve Robillard May 9 '17 at 11:05
1

This is the solution to my question:

import os
import subprocess
import sys
import warnings
import time
from threading import Timer
import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) ##Impido warning en ejecución

def blink4():#pin 7
        print ("Ejecucion iniciada...")
        time.sleep(5) ## Esperamos 1 segundo
        iteracion = 0
        while iteracion < 2: ## Segundos que durara la funcion
                GPIO.setup(4, GPIO.OUT) ## Enciendo
                print ("Encendido")
                time.sleep(5) ## Esperamos 1 segundo
                GPIO.cleanup(4) ## Apago 
                print ("Apagado")
                time.sleep(5) ## Esperamos 1 segundo
                iteracion = iteracion + 2 ## Sumo 2 porque he hecho dos parpade$
        print ("Ejecucion finalizada")

def blink23(): #pin 16
        print ("Ejecucion iniciada...")
        time.sleep(5) ## Esperamos 1 segundo
        iteracion = 0
        while iteracion < 2: ## Segundos que durara la funcion
                GPIO.setup(23, GPIO.OUT) ## Enciendo 
                print ("Encendido")
                time.sleep(5) ## Esperamos 1 segundo
                GPIO.cleanup(23) ## Apago 
                print ("Apagado")
                time.sleep(5) ## Esperamos 1 segundo
                iteracion = iteracion + 2 ## Sumo 2 porque he hecho dos parpade$
        print ("Ejecucion finalizada")

blink4()
blink23()
GPIO.cleanup() ## Hago una limpieza de los GPIO

It works right now, thank you all.

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.