1

I have a problem with my Raspberry Pi, which is connected in a relay box in GPIO7 pin. I have tested with Gpio.board and with gpio.bcm without success with 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?

lostbard
  • 5,065
  • 1
  • 15
  • 17
Meirin.f
  • 21
  • 5

2 Answers2

3

You set the pin numbering mode with GPIO.setmode(GPIO.BCM) before the loop. The first loop will work as expected, but GPIO.cleanup() will undo your setmode, and you'll get the error on the second loop.

So, just put the cleanup at the end.

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)
    time.sleep(5) 
    #GPIO.output(7, GPIO.LOW) #mode Board

GPIO.cleanup()  #devuelve los pines a su estado inicial
exit() 
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • This can seen clearly if a print statement is used inside the loop, it will successfully run once – Nick is tired May 09 '17 at 09:35
  • Thanks for your contribution but it does not work, it does not skip any errors but it does not go off and on every 5 minutes as I want and it does not go to clear. – Meirin.f May 09 '17 at 10:03
  • @Meirin.f You need a `sleep` after `GPIO.output(4, GPIO.LOW)` as well, otherwise it will immediately go HIGH again in the next loop and you'll never see it... I just added it in my answer. – Thierry Lathuille May 09 '17 at 10:18
0

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.

Meirin.f
  • 21
  • 5