Hardware application in Raspberry PI
i was in Depok yesterday , and suddenly thought about “how to control a hardware component by using PI”, because i used to control hardware device such as LED’S (Light emitting Diode) , Switches, censors, servos, and many more with microcontroller. maybe, i could do same thing by PI, then i searched for article last night , and i got brief tutorial. in this section, i would like to discuss hardware application and how to turn on / off your LED by using python script in Raspberry PI. this is interesting enough. here lists of my raw materials (depend on you) :
- PI , of course
- Resistor , 1 x 330 ohm
- Electronic Board or you can use breadboard
- Cable usually called jumper to connect electronic component to others.
- Coffee ( to enjoy step by step)
step by step :
- create electronic circuit like this and connect it to your PI. please be carefull to make this circuit, i suggest you to use breadboard.
- create file ‘lamp.py’ :
import os; #import os to use 'system' function import RPi.GPIO as GPIO #GPIO module to access your GPIO Port in PI import time GPIO.setmode(GPIO.BOARD); GPIO.setup(3, GPIO.OUT); # 3 : port GPIO3 set as output os.system("clear"); #clear screen status = True; #boolean status as flag true or false, but set true first while(status): #while status true then loop os.system("clear"); print "1. Nyalakan Lampu" print "2. Matikan Lampu" print "3. Kedap Kedip" print "0. Keluar" pil = raw_input( "Pilihan Anda : ") #store user input to pil variable if(pil=="1"): print "Lampu Dinyalakan"; GPIO.output(3,False); elif(pil=="2"): print "Lampu Dimatikan"; GPIO.output(3, True); elif(pil=="3"): for i in range(1,10): GPIO.output(3, False); time.sleep(1); GPIO.output(3, True); time.sleep(1); elif(pil=="0"): print "Terima kasih"; status = False; else: print "GAGAL"; raw_input("Press any button");
- Test your PI, execute it , type sudo python lamp.py .
- After learn about fundamental of hardware of PI, it leads you into combination of hardware and software. you can put servos, censor , switch and other technique. hopefully 🙂