Tutorial: raspberrypi-electronics-I

This is the start of my RaspberryPi tutorial series.

I will start with the basic stuff first to ensure that everyone who wants to do electronic projects is able to to it with his RaspberryPi.

All of my old electronic projects are based on the ATmega chips but it is much easier to build up a project with the Raspberry Pi because you have a full stack linux in the background if you need a bash, a network connection or higher programming languages like Javascript, Python or Ruby.

If you want to start you should do yourself a favor and buy a breadboard:

breadboard.jpg

A breadboard helps to build prototypes fast because it does not require soldering to build connections.

Additionally you need less wires to build up a circuit because most connections you want to build are allready provided by the breadboard.

The power buses (+/-) are on the left and right side. So once ground is connected to - all other elements connected to one bus are connected to the ground to.

Same with the power line.

Between the power lines there are two separated areas where all points of one line are connected to each other.

So if you put two elements in points of the same line they are connected with each other.

That's it - you know what a breadboard does.

So how does the whole setup look like?

raspberrypi-1.jpg

I use a Cobbler to connect my Raspberry Pi with the breadboard.

It helps a lot because all wires stick to the board and don't knot on the Raspberry Pi.

We should now look at the wires:

  • Orange
    • Grounds the - power line
  • 2x Yellow
    • Both yellow wires do connect the ground line to the cathode of each LED
  • 2x resistors (brown/black/yellow/silver) 1kOhm +/-10%
    • Both resistors connect the anode of each LED with the neighbor side of the breadboard
    • They "protect" the LEDs to ensure that not too much power is transfered
  • Greed and red
    • Connect the Raspberry Pi pins #23 and #24 with the lines of the anodes of the green and red LED

So what did we build?

A circuit - a closed loop of energy.

E.g. The power level of the pin #23 is set to HIGH, then the power is flowing through the resistor to the anode of the LED.

The LED emmits light and the rest of the energy is going through the cathode of the LED to the ground of the breadboard and afterwards to the ground of the Raspberry Pi.

Time to bring light to the LEDs.

Open a ssh connection to the Raspberry Pi and sudo.

All pins you want to use have to be exported to the userspace:

echo 23 > /sys/class/gpio/export
echo 24 > /sys/class/gpio/export

A gpio looks like that:

ls -al
drwxr-xr-x 3 root root    0 Feb 22 23:30 .
drwxr-xr-x 6 root root    0 Apr  9  2013 ..
-rw-r--r-- 1 root root 4096 Feb 22 23:30 active_low
-rw-r--r-- 1 root root 4096 Feb 22 23:30 direction
-rw-r--r-- 1 root root 4096 Feb 22 23:30 edge
drwxr-xr-x 2 root root    0 Feb 22 23:30 power
lrwxrwxrwx 1 root root    0 Feb 22 23:30 subsystem -> ../../../../class/gpio
-rw-r--r-- 1 root root 4096 Feb 22 23:30 uevent
-rw-r--r-- 1 root root 4096 Feb 22 23:30 value

Afterwards we have to define if the Raspberry Pi should send or receive from the pin: (we want to send = out)

echo out > /sys/class/gpio/gpio23/direction
echo out > /sys/class/gpio/gpio24/direction

Now we can enable the pins:

echo 1> /sys/class/gpio/gpio23/value
echo 1> /sys/class/gpio/gpio24/value

And disable them:

echo 0 > /sys/class/gpio/gpio23/value
echo 0 > /sys/class/gpio/gpio24/value

This can be combined to a bash script:

nano switchon.sh && chmod +x switchon.sh

Content:

#!/bin/bash
echo enable pin $1
echo $1 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$1/direction
echo 1 > /sys/class/gpio/gpio$1/value

Call it with the pin number as parameter:

bash ~/switchon.sh 23

And the green LED is shining.

That's it - the first part is finished.

Hopefully you had some fun.

Please add comments and/or suggestions - both are welcomed.