Understanding Outputs

 The Raspberry Pi has a connector that gives you access to the processor’s GPIO pins. GPIO stands for General Purpose Input Output and it means that these pins can be either an input or an output depending on how the software configures them. As well as general purpose, these can be specialised as well, like serial port pins, SPI or I2C lines.

 

 A maximum of 16mA per pin with the total current from all pins not exceeding 51mA.

Lighting Up An Led Using Your Raspberry Pi and Python

Once you’ve setup your Raspberry Pi according to my “Get started” tutorial, you are ready for your first real project. Let’s light up the RGB LED on EduBoard, using the Python programming language and the GPIO pins on your Raspberry Pi.

Assumes EduBoard already attached to Your Raspberry Pi, through the GPIO pins.

You will learn to write a simple Python program to control the circuit using the internal “Text File Editor”, called “Nano“.

Writing The Code (LED ON/OFF)

It’s important to note that when writing python scripts that utilize the GPIO pins, you must run them as a superuser or your scripts will not run properly.

 

Raspberry-Pi-GPIO-Layout-Revision-2
GPIO setup Click on the picture to enlarge.

 

The following example will lit the RGB LED red. (connected to pin number 15, GPIO 22)
Open an editor window using:

Then paste in the following text and save the file using CTRL-X and then Y and then ENTER.

You can now run the program using the command:

The LED should lit red.

The following example will turn the LED off. (connected to pin number 15, GPIO 22)
Open the editor window using:

This will open the led_on.py script in Nano (Text File Editor), ready for editing.

Edit the fourth line. Change it to:

Then  save the file using CTRL-X and then Y, change the file name to led_off.py, and then ENTER.
File Name to Write: led_off.py
Then ENTER.
Save file under DIFFERENT NAME?
Then Y, and then ENTER.

You can now run the program using the command:

The LED will turn off.

 

You can find your new LED ON/OFF scripts with the following command:ls

One of the most used commands by Linux users and the one that a Linux beginner must learn is the ls command. It is usually used to view the contents of current directory. So, when you run this command, the files and sub-directories included under the current directory will be listed. Very useful command and everyone who uses Linux should know this command

The most basic use of any command is when it is used without any option or parameter. So, when ls command is executed with no option or parameter, then it will enlist files and sub-directories of current directory.”

Syntax:

Output:

pi@raspberrypi:~$ ls
Adafruit_Python_SSD1306 button.py Fonts ks.py led_on.py python_games
button2.py Desktop ip.py led_off.py ocr_pi.png

 

Buzzer (active)


With few changes, you can use the above script to turn ON the buzzer, and also to turn it OFF.

The following example will turn ON the buzzer.(connected to pin number 22, GPIO 25)
Open the editor window using:

This will open the led_on.py script in Nano (Text File Editor), ready for editing.

Edit the third line:

and the fourth line:

Then  save the file using CTRL-X and then Y, change the file name to buzzer_on.py, and then ENTER.
File Name to Write: buzzer_on.py
Then ENTER.
Save file under DIFFERENT NAME?
Then Y, and then ENTER.

Bofore you run the buzzer_on script it is advisable to create the buzzer_off script, just in case. The buzzer is very loud.
The following example will turn OFF the buzzer.
Open the editor window using:

This will open the buzzer_on.py script in Nano, ready for editing.

Edit the fourth line. Change it to:

Then  save the file using CTRL-X and then Y, change the file name to buzzer_off.py, and then ENTER.
File Name to Write: buzzer_off.py
Then ENTER.
Save file under DIFFERENT NAME?
Then Y, and then ENTER.

Now, we should have both of the scripts, ON and OFF. Time to test the buzzer.

You can turn on the buzzer using the command:

to turn it off, use the following command:

The difference between the self drive and external drive buzzers
Cost Defective rate Frequency Convenience
Active buzzer (self drive)Used on EduBoard High High Unchangeable Need only voltage
Passive buzzer (extrenal drive) Low Low Changeable Plus oscillate circuit

Back to LED’s (Colours)

The RGB LED connected to the following GPIO pins:

  • Pin Number 15 (GPIO 22) = Red
  • Pin Number 13 (GPIO 27)= Green
  • Pin Number 11 (GPIO 17) = Blue
  • Pin Number 9 (Ground) = GND

To change the colour of the LED, simply change the GPIO setup to the required pin number. In the following example we will change the code to lit the LED green.

Open the editor window using:

This will open the led_on.py script in Nano (Text File Editor)”

Edit the third and fourth line. Change them to:

Then  save the file using CTRL-X and then Y, change the file name to led_on_green.py, and then ENTER.
File Name to Write: led_on_green.py
Then ENTER.
Save file under DIFFERENT NAME?
Then Y, and then ENTER.

You can now run the program using the command:

The LED will lit green.

To lit the LED blue, you will need to repeat these steps, with the relevant GPIO pin numbering. Make sure to save the script as follows:

Then  save the file using CTRL-X and then Y, change the file name to led_on_blue.py, and then ENTER.
File Name to Write: led_on_blue.py
Then ENTER.
Save file under DIFFERENT NAME?
Then Y, and then ENTER.

Use the LED turn off example to turn off the LED. Make sure to change the GPIO pin numbering in the scripts. Save the scripts under the following names:
led_off_red.py
led_off_green.py
led_off_blue.py

 Next Tutorial (Output LED part 2)