Last active
August 29, 2015 14:08
-
-
Save suminb/c2f48010ed27d24cbb21 to your computer and use it in GitHub Desktop.
Controlling Raspberry Pi's GPIO with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"time" | |
"github.com/davecheney/gpio" | |
) | |
func main() { | |
// set GPIO25 to output mode | |
pin, err := gpio.OpenPin(2, gpio.ModeOutput) | |
if err != nil { | |
fmt.Printf("Error opening pin! %s\n", err) | |
return | |
} | |
// turn the led off on exit | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt) | |
go func() { | |
for _ = range c { | |
fmt.Printf("\nClearing and unexporting the pin.\n") | |
pin.Clear() | |
pin.Close() | |
os.Exit(0) | |
} | |
}() | |
for { | |
pin.Set() | |
time.Sleep(100 * time.Millisecond) | |
pin.Clear() | |
time.Sleep(100 * time.Millisecond) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
PIN = 3 | |
INTERVAL = 1.0 | |
def main(): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(PIN, GPIO.OUT) | |
while True: | |
GPIO.output(PIN, 1) | |
time.sleep(INTERVAL) | |
GPIO.output(PIN, 0) | |
time.sleep(INTERVAL) | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment