Showing posts with label GPIO. Show all posts
Showing posts with label GPIO. Show all posts

Tuesday, May 26, 2015

Control an LED to be blinking ON and OFF using Python

1. Connect an LED and a resistor to the Raspberry Pi as instructions.

2. Connect the power to GPIO4 pin of Raspberry Pi instead of 3.3V. (Connect the longer leg / pin of the LED (called the anode) to the GPIO4 pin.)

3. Type the commands below:

nano blink.py

This edits the blink.py file in the nano editor. Type the code below:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.OUT)

while True:
    GPIO.output(4,1)
    time.sleep(1)
    GPIO.output(4,0)
    time.sleep(1)




Execute the Python file:

sudo python blink.py

And the LED is blinking!!



Saturday, May 23, 2015

Control an LED remotely using VNC (Virtual Network Computing)

1. Make sure an LED can be switched ON/OFF locally with a Raspberry Pi. See instructions here.

2. Connect Raspberry Pi to the internet and install the VNC server with the following command:

sudo apt-get install tightvncserver


3. Start the VNC server:

vncserver







One line of the results could be like this:

New 'X' desktop is raspberrypi:2

where 2 is the port number.


4. Add VNC viewer to Google Chrome with a PC/Mac.

Connect to Raspberry Pi's IP address, a colon character(:), and the port number. The IP address can be obtained by typing the ifconfig command or the hostname -I command in Raspberry Pi's terminal.)


Click 'Connect'
















Enter the password. (Note: If you want to change the VNC password, simply type vncpasswd command in Raspberry Pi's terminal.)

Congratulations when you can see the desktop of Raspberry Pi!!

5. Open the terminal and type in the following commands:

sudo python

>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(4,GPIO.OUT)
>>> GPIO.output(4,1)

(Note: VNC is a convenient way to take screenshot / print screen of Raspberry Pi.)

Now the LED connected to GPIO4 should be turned ON!!

Wednesday, May 20, 2015

Control an LED remotely using SSH(Secure Shell)

1. Make sure an LED can be switched ON/OFF locally with a Raspberry Pi. See instructions here.

2. Enable the SSH server using raspi-config:

Type in the terminal:

sudo raspi-config

Select 'Advanced Options'.


Select 'A4 SSH'.













Select 'Enable'.




















3. Get the IP address with the ifconfig command.

You will get the IP address such as 192.168.10.183.

4. Download and run PuTTY with a Windows PC.

5. Key in the IP address as the host name and keep the port as 22:
6. Click YES.

7. Login to Raspberry Pi.
7. Enter the commands below to control the LED to be ON/OFF:

sudo python

>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(4,GPIO.OUT)
>>> GPIO.output(4,1)

LED is turned on!


>>> GPIO.output(4,0)

And the LED is turned OFF with a remote PC!

Tuesday, May 19, 2015

Control an LED to be ON/OFF

1. Connect an LED and a resistor to the Raspberry Pi as instructions.

2. Connect the power to GPIO 4 pin of Raspberry Pi instead of 3.3V.

3. Type the commands below:

sudo python

>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(4,GPIO.OUT)
>>> GPIO.output(4,1)

 LED is ON.
 >>> GPIO.output(4,0)
LED is OFF.