Raspberry PI Switch Controller Control Code

Switch controllers can be used to turn devices on or off. They can operate a relay, or they can generate PWM signal. The On/off state can be used to turn on or turn off devices through relays, the PWM signal can be used to adjust the signal level, for example you can set the brightness of a LED or control the speed of a motor. You can learn more about PWM on the Internet. PWM in Ozeki can be adjusted in absolute numbers or as a percentage.

Required hardware

  • Raspberry PI
  • A device connected to a digital (or pwm) output

Source code to install on controller


import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(27, GPIO.OUT)

hz = input('Please define the Switch PWM frequency in Hertz(recommended:75): ')
dc = input('Please define the Switch PWM Duty Cycle: ')

switch = GPIO.PWM(27, hz)

try:
    while True:
        switch.start((dc / 2.55))

except KeyboardInterrupt:
    switch.stop()
    GPIO.cleanup()

More information