At this stage I'm going to explain how to create a file which consists of a simple code (to get a LED blinking) and we will conclude with executing the code. This will be helpful particularly at the next stage where we'll have a go with OpenCV.
First of all, breadboard blink_led.png (you can find the circuit diagram in the files)
(I assume that you have a certain level experience with breadbording circuits therefore I'm not going to get into too much detail with that.)
If you are wondering how to calculate the resistor value in this circuit, see resistor for LED calculation.pdf
See "RaspberryPi2_J8_pinout.png" for a complete RPi 2 connector J8 pinout
Alternatively see "RasPiB-GPIO_lightbox.png"
Continuing at the RPi command line: (Execute the following commands one by one)
nano blink_led.py # open the file my_blink.py with the nano editorSome of the resources use the "touch" command to create a file and then the "nano" command to edit the created file. However nano will create the file if it has not been created already, therefore there no need for the "touch" command.
Now copy / paste in blink_led.py (located at the files), then press Ctrl+O to save, then Ctrl+X to exit nano.
sudo python blink_led.pyRun the program with this command, note sudo "super user do", i.e. root access is necessary to perform hardware I/O on the RPi.
You should now see the LED on your board blinking. Press Ctrl+C to exit this program.
(For future reference Ctrl+C exits most programs when ran from a Linux command line.)
This step is absolutely crucial for does of you who wants to make the program run when Raspbian boots (i.e. for a headless embedded application) proceed as follows . . .
sudo nano /etc/rc.local # open rc.local in the nano editorIn rc.local just before "exit 0" add the following:
sudo python /home/pi/blink_led.py & # add this to rc.local, just before "exit 0"Do NOT forget the "&" to start as a separate process or the RPi will run your program indefinitely and will not continue to boot !!
Forgetting the "&" could put the RPi in an unrecoverable state (necessitating re-formatting the SD card if that's the case take a look at log 1).
/etc/rc.local is ran by the RPi as root during boot-up, so you don't really need to include "sudo" in the command even if accessing GPIO pins.
sudo shutdown -r now # reboot, the LED should start blinking during RPi boot-upTo return to the regular boot-up, simply open rc.local again and remove the "sudo python /home/pi/blink_led.py &" line
sudo nano /etc/rc.local # remove the "sudo python /home/pi/blink_led.py &"