# Pi Electronics: E1 Hello World!

The Raspberry Pi has made a massive impact in the computer science education. But lets not forget the impact it has made in electronics and the IoT community. These days with the Pi 3B+ and the Pi 4B they are getting really good-value computers, but lets not forget that these small things can be great for home servers to.

Lets look a little in the electronics world today. We’ll be using a Rpi B version (any will do) a breadboard and some electronics. This tutorial will get you started in the world of electronics using a raspberry pi and python. Note that the same can be done using other programming languages like Node-Red.

### Requirements

• Raspberry Pi 3B+ (any Pi with 40 pin headers will do)
• Ethernet/Wifi connection
• (micro) sd card with Raspberry Pi OS
• Dupont jumper wires
• Screen (optional)

### Setup

Before we can start the fun we’ll need to get the system ready.
First we’ll update our software:

sudo apt update && sudo apt upgrade -y

Depending on how long ago you update your system this can take a while.
Next is installing python, in most cases this is preinstalled but we’ll make sure it is installed like this:

sudo apt install python python-pip python-gpiozero python-gpiozero-doc -y

## Hello World

Now that our system is ready we’ll begin with setting up our electronics.
First we’ll get ourselves a red led and a resistor of 100 ohm. This resistor will limit the current going through the led. More about this in the second episode.

We connect the LED’s anode to pin 7 (GPIO4) and it’s cathode to the resistor.
The other end of the resistor we’ll connect to one of the GND pins of the Pi.
If all goes well the led should stay off at this point.

Now we’ll connect to the Pi using an SSH connection, chose your flavour program to do this, or use a keyboard an screen for direct connection.
Once in the terminal window we’ll create a new file and edit it.

nano hello_world.py

First we’ll need to tell python and the pi how to use the GPIO pins, for this we’ll import a library RPi.GPIO.
While we use this library we can use functions from this library to make our work easier.
We need to define the mode, the mode if the pin and the state of the pin. For example:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.output(7,1)

There are 2 modes available: BOARD and BMC. In board mode you’ll need to give the pin number of the connector while in BMC mode you’ll give the GPIO number.

Save the file and exit your editor. We need to run this file, for this we’ll use the python interpreter.

python hello_world.py

We now see that the LED is on. But what can we do with only turning it on? Right lets also turn it off, but without some time delay we won’t be able to see anything. To add delay’s well import another library called time and use its sleep function.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

count = 0
timeout = 1
while(count < 10):
count = count +1
GPIO.output(7,1)
time.sleep(timeout)
GPIO.output(7,0)
time.sleep(timeout)

GPIO.cleanup()

In the example above the led will flash once every 2 seconds for 10 times.

## Where to now

You’ve seen how to control a LED using the GPIO of the Raspberry Pi. But this is only an output we are controlling, in many cases you’ll want an input to the controller to.
In the next Episode we’ll take a further look in how to control a led by using a switch.

If you have any question, pointers or remarks. Head over to our contact page and contact us from there. Otherwise I’ll see you in our next tutorial?