I'm doing a project which I got to move two motors and they have differents movements. There are the codes:
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'
import sys
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(19, GPIO.OUT) ## Setup GPIO Pin 11(motor b enable) to OUT
GPIO.setup(16, GPIO.OUT) ## Setup GPIO Pin 11(motor a enable) to OUT
GPIO.setup(22, GPIO.OUT) ## Setup GPIO Pin 11(motor a control) to OUT
GPIO.setup(18, GPIO.OUT) ## Setup GPIO Pin 11(motor a control) to OUT
GPIO.setup(23, GPIO.OUT) ## Setup GPIO Pin 11(motor b control) to OUT
GPIO.setup(21, GPIO.OUT) ## Setup GPIO Pin 11(motor b control) to OUT
GPIO.output(16, False) ## disable motor a
GPIO.output(19, True) ## enable motor b
p=GPIO.PWM(19,50) ## frequency 50
p.start(1)
try:
while True:
GPIO.output(22, False) ## dont run motor a
GPIO.output(18, False) ## dont run motor a
GPIO.output(23, True) ## run motor b
GPIO.output(21, False) ## run motor b
p.ChangeDutyCycle(35) ## duty cycle 10%
except KeyboardInterrupt:
pass
p.stop()
GPIO.cleanup()
sys.exit()
Code #2
import RPi.GPIO as GPIO
from time import sleep
import sys
GPIO.setmode(GPIO.BOARD)
Motor1A = 22
Motor1B = 18
Motor1E = 16
Motor2A = 23
Motor2B = 21
Motor2E = 19
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)
print "Going forwards"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)
GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
GPIO.output(Motor2E,GPIO.HIGH)
sleep(2)
print "Now stop"
GPIO.output(Motor1E,GPIO.LOW)
GPIO.output(Motor2E,GPIO.LOW)
GPIO.cleanup()
sys.exit()
I want to mix both codes in one. I mean, convert these two codes in one which has two differents speed.
Or If someone knows another code I'll be very thankful
How do I do that?
Seriously, use gpiozero, it's a lot more fun and turns your code into:
from gpiozero import Motor
from time import sleep
motorA = Motor(22, 18) # your gpio pins go here
motorB = Motor(23, 21)
motorA.forward(0.5) # half speed
motorB.forward(0.5)
sleep(2) # wait 2 seconds before stopping the motors
motorA.stop()
motorB.stop()
Then, you can go a step further and use the Robot class instead (Documentation here), and make everything even easier:
from gpiozero import Robot
from time import sleep
robot = Robot(left=(22, 18), right=(23, 21))
robot.forward(1) # full speed
sleep(2)
robot.stop()
The robot class also has some more functions, for example:
robot.left()
robot.right()
Most likely, you'll have to install the library first if you haven't used it yet. Type the following on the terminal in order to do so.
pip install gpiozero
Related
I am attempting to create a daemon that will execute my script at boot.
using this code as my template Running a python script
my python script works interactively when a user is logged in.
def wait_for_network():
while os.system("ping -c 1 8.8.8.8") != 0:
time.sleep(1)
return
from getmac import get_mac_address
from datetime import datetime
import RPi.GPIO as GPIO
import os
import time
import random
import RPi.GPIO as GPIO
import requests
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(14, GPIO.IN)
eth_mac = get_mac_address()
#print(eth_mac)
API_ENDPOINT = "https://xxxxxx.com/handlers/receiveStatus.ashx"
CUSTOMER_KEY = "1234567890"
# Define a callback function that will be called by the GPIO
# event system:
def onButton(channel):
if channel == 14:
dt_string = (datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3])
data = {'ID':CUSTOMER_KEY,
'UUID':str(eth_mac),
'DT':dt_string,
'A':str(GPIO.input(6)),
'B':str(GPIO.input(24)),
'C':str(GPIO.input(23)),
'D':str(GPIO.input(5))
}
r = requests.post(url = API_ENDPOINT, data = data)
#print r.text
#print data
GPIO.add_event_detect(14, GPIO.RISING, callback=onButton, bouncetime=20)
#input()
My question is this - the #input() do i need it when running as a daemon?
With it, the script runs until the users presses ctrl-c to break out of.
When I comment it out, the script runs once then returns to the prompt.
The GPIO is making a thread and the main thread needs to wait for it. That was done with the input(). What you can do instead is to make a loop that sleeps instead in place of the input().
while True:
time.sleep(1)
That will hold the process from exiting until a ctrl c happens.
I explain my need : i wish to run ffmpeg with a python script (that's ok) but i need to know of the script is launched with a blink led connected on the GPIO of my RPI, But i dont know why i can launch my script and start le blink (or just a led on)
Can u help me ? show me the light, please ;)
import RPi.GPIO as GPIO
import time
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22,GPIO.IN)
# fonction qui fait clignoter la led
def blink(led):
GPIO.output(led,True)
time.sleep(0.5)
GPIO.output(led,False)
time.sleep(1)
# input of the switch will change the state of the LED
while 1:
if ( GPIO.input(22) == True ):
print "start broadcast"
os.system("sudo /home/pi/videopi/webcam.sh")
blink(4) << not ok like this !
time.sleep(1)
Assuming your os script runs successfully (I would recommend subprocess instead), what you're describing is called concurrency -- https://realpython.com/python-concurrency/
I would structure your code like this:
import RPi.GPIO as GPIO
import time
import os
from threading import Thread
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22,GPIO.IN)
# fonction qui fait clignoter la led
def blink(led):
while True:
GPIO.output(led,True)
time.sleep(0.5)
GPIO.output(led,False)
time.sleep(1)
# input of the switch will change the state of the LED
while True:
if ( GPIO.input(22) == True ):
blinking = Thread(target=blink, args=(4,)) # create thread
blinking.start() # start blinking
print("start broadcast")
os.system("sudo /home/pi/videopi/webcam.sh")
blinking.join() # stops blinking once webcam.sh completed
print("broadcast complete")
#import GPIO library
import RPi.GPIO as GPIO
#set GPIO numbering mode and define input pin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
try:
while True:
if GPIO.input(16)==0:
print "Open_16"
else:
print "Closed_16"
finally:
#cleanup the GPIO pins before ending
GPIO.cleanup()
how do i modify the above script to monitor 2 GPIO pins
16 is being monitored how to i monitor pin 16 and say pin 33
thanks for any help
#import GPIO library
import RPi.GPIO as GPIO
#set GPIO numbering mode and define input pin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
GPIO.setup(33,GPIO.IN)
try:
while True:
if GPIO.input(16)==0:
print "Open_16"
else:
print "Closed_16"
if GPIO.input(33)==0:
print "Open_33"
else:
print "Closed_33"
finally:
#cleanup the GPIO pins before ending
GPIO.cleanup()
If you want logical relations between the states of the two pins like AND, OR, XOR, ... use logical operators ( https://www.guru99.com/python-operators-complete-tutorial.html )
I would like to run my a function within my python program when the time is 7am. Almost like a when function when time == time:
I don't want to use cron because I want this to be internal function not executing the whole script
Here is my python script I have created:
#sprinkler.py file for sprinkler system
#this is the main file for the sprinklerOS
#functions to import for our test_run.py file
import RPi.GPIO as GPIO
import time
import argparse
import sys
import datetime
#how many zones the user/YOU have set up
zone_count = 10
#gpio pins on the raspberry pi for sprinkler system
pin = [12,7,16,11,18,13,22,15,32,29]
#set mode to board meaning the pin number on pi (1-40) instead of gpio number
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#setting up all of the lights or relays as outputs(in a while loop)
for a in range(10):
GPIO.setup(pin[a],GPIO.OUT)
GPIO.output(pin[a], GPIO.LOW)
def run_sprinklers():
for b in range(zone_count):
run_time = 5
GPIO.output(pin[b], GPIO.HIGH)
time.sleep(run_time)
GPIO.output(pin[b], GPIO.LOW)
time.sleep(1)
I want to run_sprinklers() when the time is 7am
Thank you in advanced
As others have mentioned in the comments, cron is probably the best solution. However if this is a persistent process and you cannot rely on a tool like cron the schedule module, https://github.com/dbader/schedule, might be of interest to you.
import RPi.GPIO as GPIO
import time
import argparse
import sys
import datetime
import schedule
#how many zones the user/YOU have set up
zone_count = 10
#gpio pins on the raspberry pi for sprinkler system
pin = [12,7,16,11,18,13,22,15,32,29]
#set mode to board meaning the pin number on pi (1-40) instead of gpio number
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#setting up all of the lights or relays as outputs(in a while loop)
for a in range(10):
GPIO.setup(pin[a],GPIO.OUT)
GPIO.output(pin[a], GPIO.LOW)
def run_sprinklers():
for b in range(zone_count):
run_time = 5
GPIO.output(pin[b], GPIO.HIGH)
time.sleep(run_time)
GPIO.output(pin[b], GPIO.LOW)
time.sleep(1)
schedule.every().day.at("7:00").do(run_sprinklers)
while True:
schedule.run_pending()
time.sleep(1)
I just bought a Raspberry Pi and I was playing around with an LED, trying to learn Python. So my setup is as follows: my led is is connected to the 7th PIN of my GPIO and to ground. I made the following code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.output(7, True)
time.sleep(10)
GPIO.output(7, False)
time.sleep(5)
GPIO.output(7, True)
When I ran this code, the LED blinks once for 10 seconds, turns off and nevers turns back on. What can be wrong?
Try this:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
while True:
GPIO.output(7, True)
time.sleep(10)
GPIO.output(7, False)
time.sleep(5)
It should loop the on/off sequence, causing the light to turn on for 10s, then turn off for 5s, and repeat.