def get_connection(self, chnl=None): if chnl < 0 or chnl >= self._channels: raise ValueError("Invalid channel value {} for expander {} {}".format(chnl, self._chip, self._name)) chip = self._chip if chip == 'mcp3001': return gpiozero.MCP3001(channel=chnl) elif chip == 'mcp3002': return gpiozero.MCP3002(channel=chnl) elif chip == 'mcp3004': return gpiozero.MCP3004(channel=chnl) elif chip == 'mcp3008': return gpiozero.MCP3008(channel=chnl) elif chip == 'mcp3201': return gpiozero.MCP3201(channel=chnl) elif chip == 'mcp3202': return gpiozero.MCP3202(channel=chnl) elif chip == 'mcp3204': return gpiozero.MCP3204(channel=chnl) elif chip == 'mcp3208': return gpiozero.MCP3208(channel=chnl) elif chip == 'mcp3301': return gpiozero.MCP3301(channel=chnl) elif chip == 'mcp3302': return gpiozero.MCP3302(channel=chnl) elif chip == 'mcp3304': return gpiozero.MCP3304(channel=chnl) return None
def GetApparentWind(): Winkelaufnehmer_Objekt = gpiozero.MCP3008( channel=0) # Objekt mit Wert aus AD-Wandler Kanal 0 Winkelaufnehmer_Wert = Winkelaufnehmer_Objekt.value # Der Winkelaufnehmer (Contelec) gibt Werte von 5-95 Prozent der Referenzspannung an, und das # sehr praezise. Deshalb alles unter- und oberhalb auf 0 resp. 360 Grad. if Winkelaufnehmer_Wert <= 0.05: GemessenerWinkel = 0 elif Winkelaufnehmer_Wert >= 0.95: GemessenerWinkel = 360 else: Rohwert_bereinigt = Winkelaufnehmer_Wert - 0.05 # somit Werte von 0-0.9, damit Dreisatz: GemessenerWinkel = (360 / 0.9) * Rohwert_bereinigt GemessenerWinkel += offsetwind_bootausrichtung if GemessenerWinkel > 360: GemessenerWinkel -= 360 elif GemessenerWinkel < 0: GemessenerWinkel += 360 # Damit die Werte eindeutig sind, wird 360 auf 0 gesetzt. if GemessenerWinkel == 360: GemessenerWinkel = 0 return GemessenerWinkel
def __init__( self, contactSwitch=[2, 3], ledVolume=4, potVolume=17, motorDirection=[18, 23, 24], muteSwitch=25, ): self.contactSwitch = [ gpiozero.Button(contactSwitch[0], bounce_time=self.BOUNCE_TIME), gpiozero.Button(contactSwitch[1], bounce_time=self.BOUNCE_TIME) ] self.ledVolume = gpiozero.PWMLED(ledVolume) self.potVolume = gpiozero.MCP3008(channel=potVolume) # TODO program motorDirection[2] for PWM speed control # see L298N motor driver documentation for details self.motorDirection = [ gpiozero.Motor(motorDirection[0], motorDirection[1], pwm=False), gpiozero.PWMOutputDevice(motorDirection[2]) ] # This is a toggle switch not a button self.muteSwitch = gpiozero.Button(muteSwitch, bounce_time=self.BOUNCE_TIME) self.strikeState = 'idle'
def GetDistance(): IR_Sensor_Objekt = gpiozero.MCP3008( channel=1) # Objekt mit Wert aus AD-Wandler Kanal 1 IR_Sensor_Wert = IR_Sensor_Objekt.value IR_Sensor_Wert_handlich = int(IR_Sensor_Wert * 100) # ergibt return-Wert zw. 0 und 100 # !! Achtung. Diese Werte sind aber nicht linear, sondern werden bei uns nur fuer den Vergleich # mit einem Cutoff verwendet und nicht zur Distanzbestimmung in Meter oder so ... return IR_Sensor_Wert_handlich
def _setup_hardware(self): """ Create instances of all peripherals needed. """ self._main_LED = gpiozero.RGBLED(12,13,19,pwm=True) self._piezo = gpiozero.PWMOutputDevice(18,frequency=400) self._sensor_adc = gpiozero.MCP3008(port=0,device=0,channel=3) self._button = gpiozero.Button(4,hold_time=3,hold_repeat=False,pull_up=True) self._button.when_pressed = self._on_button_press self._button.when_released = self._on_button_release self._button.when_held = self._on_button_hold
def __init__(self, steprefresh=1): # Variables self.servotime = 0.5 self.steptime = 0.01 # 100 Hz step frequency self.steprefresh = steprefresh # timespan of stepping sequence [s] self.just_length = 24 # justification length # Initialization self.SERVO_1 = gpiozero.Servo(24) self.SERVO_2 = gpiozero.Servo(25) self.MOSFET1_G = gpiozero.DigitalOutputDevice(12, initial_value=False) self.MOSFET2_G = gpiozero.DigitalOutputDevice(16, initial_value=False) self.MOSFET3_G = gpiozero.DigitalOutputDevice(20, initial_value=False) self.MOSFET4_G = gpiozero.DigitalOutputDevice(21, initial_value=False) self.COIL_1 = gpiozero.DigitalOutputDevice(17, initial_value=False) self.COIL_2 = gpiozero.DigitalOutputDevice(27, initial_value=False) self.COIL_3 = gpiozero.DigitalOutputDevice(22, initial_value=False) self.COIL_4 = gpiozero.DigitalOutputDevice(5, initial_value=False) self.LIGHT = gpiozero.DigitalOutputDevice(13, initial_value=False) self.BUZZER = gpiozero.DigitalOutputDevice(6, initial_value=False) self.SWITCH_1 = gpiozero.Button(14) self.SWITCH_2 = gpiozero.Button(15) self.SWITCH_3 = gpiozero.Button(18) self.SWITCH_4 = gpiozero.Button(23) self.SWITCH_5 = gpiozero.Button(26) # BCM 26 (Mission Cont Shutdown) self.SWITCH_6 = gpiozero.Button(19) # BCM 19 (Limit switch input) self.BATTERY = gpiozero.MCP3008(channel=0) self.AMBIENT = gpiozero.MCP3008(channel=1) self.BUTTON1 = gpiozero.MCP3008(channel=2) self.BUTTON2 = gpiozero.MCP3008(channel=3) self.POTMETR = gpiozero.MCP3008(channel=4) self.SWITCH_7 = gpiozero.MCP3008(channel=5) # Spare limit switch input self.SWITCH_8 = gpiozero.MCP3008(channel=6) # Spare limit switch input self.COILS_FWD = [self.COIL_1, self.COIL_2, self.COIL_3, self.COIL_4] self.COILS_REV = [self.COIL_4, self.COIL_3, self.COIL_2, self.COIL_1] self.SWITCHES = [ self.SWITCH_1, self.SWITCH_2, self.SWITCH_3, self.SWITCH_4 ] self.MOSFETS = [ self.MOSFET1_G, self.MOSFET2_G, self.MOSFET3_G, self.MOSFET4_G ]
def heartAnalyzer( ): # records heartrate and adds the change of signal to monitor change of heartrate hr = gpiozero.MCP3008(channel=1) print("start") counter = 0 prevBeat = .748 # idling value is around .748 diffBeat = 0 currentSpike = 0 while True: counter = counter + 1 diffBeat = prevBeat - hr.value prevBeat = hr.value # prevBeat gets rewritten by current value here if (counter <= 5000): currentSpike = currentSpike + abs(diffBeat) else: counter = 0 print("end") return currentSpike
#!/usr/bin/env python #-*- coding: utf-8 -*- # Lecture entrees analogique A4 a A7 # Read analog input from A4 to A7 import gpiozero as g import time, math inputs = [g.MCP3008(channel=ch, device=0) for ch in (4, 5, 6, 7)] while True: # entree analogique retourne une valeur entre 0 et 1 # analog input returns a value between 0 and 1 # Augmenter l echelle de valeur entre 0 et 1024 # Just scale them up from 0 to 1024 values = list([math.trunc(input.value * 1024) for input in inputs]) print("A4=%4i, A5=%4i, A6=%4i, A7=%i" % (values[0], values[1], values[2], values[3])) time.sleep(0.200)
# Example file for LinkedIn Learning Q2-12 analog to digital # uses MCP3008 import gpiozero import time analogToDigital = gpiozero.MCP3008(channel = 0) while True: print(analogToDigital.value) time.sleep(2)
# A function to send email whenever I call it. Email's message is the variable # placed in the brackets when the function is called def alert(message): #using with means the connection to the server is automatically closed with SMTP_SSL("smtp.gmail.com", port, context=context) as server: server.login(sender_email, password) # Logs into gmail server server.sendmail(sender_email, receiver_email, message) # Sends the email to the set address alert(test) print("Test email sent") # GPIO ldr = gpio.MCP3008( 0 ) # Light Dependant Resistor connected to the MCP3008 Analogue to Digital converter. pir = gpio.MotionSensor(4) button = gpio.Button(26, pull_up=False) while True: # Infinite loop while pir.value == 0 and ldr.voltage < 2: #value of 2V chosen as threshold as when lights on value is typically 1.2V & lights off >3 print("conditions met") print("PIR: ", pir.value, " LDR: ", ldr.voltage) print("counting 90s to see if conditions remain the same") sleep(90) alert( "User, you left your lights on. Turn them off to avoid wasting energy" ) button.wait_for_press(86400) #number is a timeout of 24h in seconds
#!/usr/bin/env python #-*- coding: utf-8 -*- # Lecture joystick analogique # analog joystick read import gpiozero as g import time, math vert = g.MCP3008( channel=1, device=0 ) horz = g.MCP3008( channel=0, device=0 ) while True: # vert.value entre 0 et 1 # vert.value between 0 and 1 v = math.trunc(vert.value * 1024) h = math.trunc(horz.value * 1024) print( "Joystick H=%4i, V=%4i" % (h,v) ) time.sleep(0.200)
import time, gpiozero adc = gpiozero.MCP3008(channel=0) while True: voltage = adc.voltage print("Spannung am Wassersensor: %.2f V" % voltage) time.sleep(1)
#!/usr/bin/env python #-*- coding: utf-8 -*- # Lecture PhotoRésistance et Potentiomètre # LDR & pot reading import gpiozero as g import time, math ldr = g.MCP3008(channel=2, device=0) pot = g.MCP3008(channel=3, device=0) while True: # ldr.value entre 0 et 1 # lcd.value between 0 and 1 ldr_value = math.trunc(ldr.value * 1024) pot_value = math.trunc(pot.value * 1024) print("ldr=%4i, pot=%4i" % (ldr_value, pot_value)) time.sleep(0.200)
COIL_1 = gpiozero.DigitalOutputDevice(17, initial_value=False) COIL_2 = gpiozero.DigitalOutputDevice(27, initial_value=False) COIL_3 = gpiozero.DigitalOutputDevice(22, initial_value=False) COIL_4 = gpiozero.DigitalOutputDevice(5, initial_value=False) IR_LED = gpiozero.DigitalOutputDevice(6, initial_value=False) BUZZER = gpiozero.DigitalOutputDevice(13, initial_value=False) COLORS = gpiozero.DigitalOutputDevice(19, initial_value=False) SWITCH_1 = gpiozero.Button(14) SWITCH_2 = gpiozero.Button(15) SWITCH_3 = gpiozero.Button(18) SWITCH_4 = gpiozero.Button(23) SWITCH_5 = gpiozero.Button(26) # Main board switch BATTERY = gpiozero.MCP3008(channel=0) AMBIENT = gpiozero.MCP3008(channel=1) BUTTON1 = gpiozero.MCP3008(channel=2) BUTTON2 = gpiozero.MCP3008(channel=3) POTMETR = gpiozero.MCP3008(channel=4) COILS_FWD = [COIL_1, COIL_2, COIL_3, COIL_4] COILS_REV = [COIL_4, COIL_3, COIL_2, COIL_1] SWITCHES = [SWITCH_1, SWITCH_2, SWITCH_3, SWITCH_4] MOSFETS = [MOSFET1_G, MOSFET2_G, MOSFET3_G, MOSFET4_G] # FUNCTIONS def stepper_fwd(stepcount): for i in range(1, stepcount): for coil in COILS_FWD:
#!/usr/bin/env python import socket import lms import sys import gpiozero import time from pprint import pprint playerName = socket.gethostname() server = lms.find_server() onSwitch = gpiozero.Button(17) volumePoti = gpiozero.MCP3008(channel=0, device=1) multiSwitch = gpiozero.MCP3008(channel=1, device=1) default_playlist = "SWR1 Baden-Wuertenberg" def getPlaylistURI(pl): try: uri = server.query('playlists', 0, 1, dict(search=pl, tags="u"))['playlists_loop'][0]['url'] except: uri = None return uri # find the default playlist default_uri = getPlaylistURI(default_playlist)
import time, gpiozero poti = gpiozero.MCP3008 (channel = 0) while True : wert = int((poti.raw_value / 1023) * 1000) print ("Der Poti steht auf" , wert , "Ohm") time.sleep (1)
import gpiozero import warnings from time import sleep warnings.filterwarnings('ignore') sensor = gpiozero.MCP3008(channel=0) sensor_power = gpiozero.DigitalOutputDevice(4) while True: sensor_power.on() sleep(0.02) print(sensor.value) sensor_power.off() sleep(10)
import gpiozero as zero from time import sleep if __name__ == "__main__": mcp3008 = zero.MCP3008(7) while True: print("the channel 7 vlaue is {:.2f}".format(mcp3008.value)) sleep(1)