If you think the Arduino was cool, just wait until you get a Raspberry Pi — these things are amazing. In addition to being fully functional computers, they also have a set of pins general purpose I/O . Just like the Arduino, we can use them to create electronics projects — and it’s surprisingly easy to get started.

Today we will connect a relay and use it to turn on an LED, but you can also easily turn on a lamp.

This introduction serves as the first part of a larger home automation project. Christian has already laid the foundations for getting started with the Pi so I won’t repeat the initial steps of booting the OS or connecting the SD card — I’m assuming you already have running Raspian system .

Warning : though Raspberry Pi can use similar to Arduino, needs a little more attention. The Pi contacts work at voltage 3.3 V while the Arduino uses 5V. While the Arduino can be broken, breaking the Pi is much easier since the pins are directly connected to the onboard chip — sending 5V down can fry it.

So be very careful when trying to reproduce Arduino projects on your Pi — either follow the specific Pi manual or make sure you have a good grasp of basic electronics.

Requirements:

  • NPN transistor such as P2N2222A
  • Resistor 1k
  • Relay; I’m using a 4-relay 5v module which has an additional built-in protection circuit (so no need for additional diodes
  • LED and 220 ohm resistor for testing
  • Lay cable

Splitter cable («Cobbler Kit»)

The GPIO pins are located on the side of the Pi, next to the RCA video out connector.

breakout cable

While you could technically plug some female-ended patch cables directly into them, they’re not labeled in any useful way, and so you’re more likely to break something. Instead, get a breakaway cable like one from Adafruit, or one of the many cheaper clones on eBay. You may need to solder this as it will come in kit form.

breakout board

While there is a notch on the side of the board to indicate proper cable placement, the Pi side does not. Make sure the 3v and 5v pins on the far corner of the Pi are aligned with the corresponding pins on the board. Of course, if you bought a case that doesn’t show the GPIO header, you’ll need to work with a bare Pi or cut a hole.

clear case, GPIO

Alternatively, you can get a complete board that sticks on top of your Pi and usually comes with a bunch of useful components.

main circuit

Set up the schema as described below. I omitted the relay diagram as this will vary. Use terminals NO (normally closed) and COM your relay for your LED or other device.

scheme

Use pin 4 from the Raspberry Pi. On my discussion panel, it is labeled +GPCLK0; regardless, this is the fourth pin, counting from 3V3.

pin4

Command line testing

In the following examples, I assume that you are logged in via SSH or otherwise as root. If not, you will need to enter some of the commands with sudo to elevate privileges.

First we need to install WiringPi .

git clone git://git.drogon.net/wiringPi cd wiringPi ./build 

Assuming everything went well, we should now be able to directly control the GPIO from the command line like so:

  gpio -g mode 4 out
 gpio -g написать 4 1 

The first command is similar to the Arduino setup function where we tell the Pi to use pin 4 as an output. The following command writes a value of 1 to the pin, which should fire the transistor, activate the relay, complete the circuit for the LED, and turn it on.

Stunning. If it doesn’t, go back and check your wiring. Relay clicking?

python

While Python is not the only way to communicate with GPIO pins, it is generally considered to be the simplest and most commonly seen in existing designs. Unlike C, Python is relatively easy to pick up (here are 5 sites to learn Python sites to learn Python programming).

Start by installing the following Python extensions:

 apt-get install python-dev apt-get install python-rpi.gpio 

Now create a new file named test.py. if you are using the command line, type

 nano test.py 

Paste or type the following (also in this box):

 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.OUT) for x in range(0,10): time.sleep(5) GPIO.output(4,1) time.sleep(5) GPIO.output(4,0) 

This is a very simple python script that turns on an LED (or whatever you have connected to your relay) for 5 seconds, then off for 5 seconds, 10 times . You should be able to understand most of the code. GPIO.setmode line is simply used to specify the pin numbering scheme we will be using.

This is it! To run the code, type:

 python test.py 

finished

We’ll be working on the setup a bit next week and doing some cool stuff like adding voice control. If you are going to add high voltage electrical components to a relay circuit, you need to be sure you are using the correct rated relay. on the wire under tension, and make sure everything is enclosed so that it does not fall under prying fingers. babies or mothers. But seriously, be safe.

Please leave your questions, comments, feedback, and haikus in the box below — but remember that I will be judging you on grammar use.

Похожие записи