Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python is ignoring time.sleep(1)

Hi I am trying to run a small python program to control the gpio headers on the raspberry pi and I want it to wait 1 second before turning on each led but instead of sleeping in between each led it waits and sleeps at the end instead (it sleeps for the total 3 seconds which is all the seconds added up).Here is the code I am using

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(True)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)

print"Lights"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)

GPIO.output(23,GPIO.HIGH)
time.sleep(1)

GPIO.output(24,GPIO.HIGH)
time.sleep(1)

GPIO.cleanup()
like image 660
zcarp1020 Avatar asked Sep 22 '26 00:09

zcarp1020


2 Answers

It appears that

GPIO.setup(18,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)

is turning on your LED's, not

GPIO.output(24,GPIO.HIGH)

Make sure you put your print statements between the GPIO.OUT statements like so:

GPIO.setup(18,GPIO.OUT)
time.sleep(1)
GPIO.setup(23,GPIO.OUT)
time.sleep(1)
GPIO.setup(24,GPIO.OUT)
like image 187
14 revs, 12 users 16% Avatar answered Sep 24 '26 12:09

14 revs, 12 users 16%


The initial delay may just be setup time.

Based on http://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/ try

GPIO.setup(channel, GPIO.OUT, initial=GPIO.LOW)

To set them initially off.

i.e.

GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(24, GPIO.OUT, initial=GPIO.LOW)
like image 25
Jesse Vogt Avatar answered Sep 24 '26 12:09

Jesse Vogt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!