Setting up a 5v Relay

Often you may wish to control modules with a higher voltage with the Raspberry Pi. For this purpose, relays can be used on the Raspberry Pi. The relay is essentially a switch that is switched by means of a low-voltage pulse. Relay modules generally come in sizes ranging from 1 channel to 16 channels. Relays are widely used on Raspberry Pi projects that involve home automation. For example, switching on and off a light using voice commands.

Equipment List

  • Any Raspberry Pi with GPIO pins – 3B/3B+ recommended
  • Jumper Wires
  • 12v DC Switching Power Supply
  • A 12v device to test with, i.e PC fan, motor, LED strip etc
  • Relay

Setting up the Hardware

In the setup in the diagram below, we demonstrate using a 12v PC fan.

Complete the below jumper cabling plan between the Pi, power supply, relay and fan/device you choose to use. Some people may find it easier to follow the image above, rather than the cable map below. They’re identical.

Cable Map:

Pin 2 (5v) on Pi <=> VCC on relay
Pin 6 (ground) on Pi <=> GND on relay
Pin 40 (BCM21) on PI <=> IN1 on relay
Negative wire from fan <=> Negative terminal on 12v DC power supply
Positive wire from fan <=> Normally Open (NO) terminal of relay
Positive wire from 12v DC power supply <=> Common (C) terminal of relay (the middle one)

Setting up the Software

By following the above hardware setup exactly, you have chosen BCM21 as the GPIO pin for switching the relay on/off.

We need to ensure the Pi is up to date and has some essential software installed.

sudo apt-get update
sudo apt-get install nano

Create a file called script.py in /home.pi

cd /home/pi
nano script.py

Paste the below code inside and save the file.

import RPi.GPIO as GPIO
import time

#GPIO pin number
channel = 21

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)

# Turn fan on
def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH) 

# Turn fan off
def motor_off(pin):
    GPIO.output(pin, GPIO.LOW) 


if __name__ == '__main__':
    try:
        motor_on(channel)
        time.sleep(2)
        motor_off(channel)
        time.sleep(2)
        GPIO.cleanup()
    except KeyboardInterrupt:
        GPIO.cleanup()

Run the script

python script.py

Raspberry Pi 3B vs 3B+ Comparison

The Raspberry Pi Foundation has released the Raspberry Pi 3B+, an updated version of the RPi 3B. Its predecessor, the 3B, has now been available on the market for around two years and everyone has been waiting patiently for an updated version.

The 3B+ is the same size as its predecessor. This means that the existing cases and most other accessories can continue to be used without any problems. Much else remains the same and the differences are easily manageable. Most of the changes were in the technical specification.

Technical Differences

3B3B+
Release dateFebruary 2016March 2018
Size85.6 × 56mm85.6 × 56mm
SOCBCM2837BCM2837
CPUARM Cortex-A53
(ARMv8-A)
ARM Cortex-A53
(ARMv8-A)
CPU cores 44
CPU clock speed4 x 1200 MHz4 x 1400 MHz
USB4 x USB 2.04 x USB 2.0
AudioHDMI (digital)
3,5mm jack
HDMI (digital)
3,5mm jack
Network10/100 MBit 10/100/1000 MBit 
WLAN2,4 GHz WLAN b/g/n2,4/5 GHz WLAN ac
BluetoothBluetooth 4.1Bluetooth 4.2
GPIO pins4040
PoENoYes
Maximum power input4.4w7w
Power source5V Micro USB
min. 2.5A
5V Micro USB
min. 2.5A

What’s new in the Raspberry Pi 3B+?

In order of importance (in our opinion):

Faster Network Speeds
In the new LAN7515 chip, Gigabit Ethernet is finally supported. However, you cannot expect the full gigabit data. The reason for this is due to the fact that the gigabit ethernet chip hangs internally on a USB 2.0 controller, so 315 Mbit/s is the maximum. In its predecessor, the Raspberry Pi 3B, the speed was only 95 Mbps. Still a very nice improvement.

Higher CPU Clock Speed
The four ARM cores have also been improved. Now, the clock is set at 1.4 GHz by default, instead of 1.2 GHz and is therefore around 17% faster. A metal lid now serves as a heat spreader to better dissipate the heat produced by the increased clock frequency. There is also a similar metal sheet lid on the GPU. A slight drawback is that the higher clock speed also results in higher power consumption.

5Ghz Wireless
The Broadcom BCM43455 is a new WLAN controller that supports dual-band ac WLAN for 5 GHz wireless, however, only one antenna is installed. For this reason, throughput is only increased moderately.

Power-over-Ethernet
There are now 4 pins for PoE on the board. A PoE HAT for the Raspberry Pi, which can supply the Raspi with up to 12.95 watts (IEEE 802.af). The PoE Hat is now available to purchase here.

Power Consumption & Temperature

Power consumption and running temperatures of the new upgraded model has been the biggest topics, so we decided to run some tests and record our findings.

The setup:

  • Raspberry Pi 3B & 3B+ without case
  • Network via cable to GBit switch
  • OS: Raspbian Stretch Lite
  • Two measurements: Idle and under full load using (“sysbench –test=cpu –cpu-max-prime=25000 –num-threads=8 run“)
  • Room temperature: approx. 21°C
3B3B+
Watt, Idle1.2w2.2w
Watt, Full Load3.7w5.4w
Temperature, Idle43°C39°C
Temperature, Load74°C65°C

Test conclusion:

The RPi 3B+ has a significantly higher power consumption when idling, approximately 80% higher than the 3B. At full load, it is approximately 45% higher than the 3B. However surprisingly, thanks to the new metal heat spreaders, the RPi 3B+ is around 10°C cooler than its previous model. Since the Raspberry Pi throttles the clock rate if the temperature of 85°C is reached, the lower temperature of the 3B+ is a huge positive. This means that the extra power can really be called up without it getting into throttling or requiring noisy and space consuming cooling devices.

The Golden Question – Is it worth upgrading?

Yes and no. It really depends on what the Raspberry Pi in question is being used for. If the focus of the project is on CPU performance, for example a media centre, the 3B+ will perform significantly better. The same applies to high network requirements. If however the project is focused on energy saving and requires only light performance, stick with the 3B.

That being said, if you need a new Pi or are looking to get one for the first time, the sale price of both models are the same, so grab the 3B+!

Headless Raspberry Pi Setup

Setting up a Raspberry Pi is easy. Setting up a headless Raspberry Pi should take no longer than 30 minutes. There is no requirement for a screen, keyboard or mouse, hence the use of the word headless. This option is perfect for setting up your Pi for something like a web server, Minecraft server, file server etc.

Equipment/Software List

Equipment:

  • Raspberry Pi 3B/3B+ recommended
  • Micro USB Power Supply
  • 8GB+ MicroSD Card/8GB+ USB Flash Drive (if you are using 3B+)
  • A computer with MicroSD slot/adapter (if you are using a MicroSD card)

SD Card Setup

You will also need to download the latest version of Raspbian. We strongly recommend the Stretch Lite version if you do not need a GUI. Raspbian Stretch Lite is lightweight and downloads pretty fast on a modern connection.

Once the image is downloaded and decompressed, you will have to install it on the SD card. Download and install Etcher Balena. It’s a free software that allows you to unpack and install the ISO on your SD Card. It’s available on Windows, Mac and Linux.

Once Etcher is installed, plug in your SD card or USB flash drive. Open Etcher. Click “Select Image” and choose the Raspbian ISO you downloaded. Next, click “Select Drive” and choose the SD card you just plugged in. Finally, press “Flash!”. This should take 5-10 minutes to complete. Take a short break, you deserve it.

Enabling Wifi & SSH

Once Etcher tells you the process is complete, close Etcher but keep the SD card plugged in. Navigate to the SD card and open it via This PC (for Windows) or Finder (for Mac). It’s usually labelled “BOOT”.

Create a blank file in the root directory of the SD card called “ssh”. This file must be empty and only have the letters “ssh” in the file name – no file extension. This will tell the Pi to enable SSH when you first boot the Pi.

Finally, we need to create a file called wpa_supplicant.conf in the root directory of the SD card, just like we did for the ssh file. Copy the below configuration and paste it into the wpa_supplicant.conf file. You must update the country code on the first line to your country. Use your countries ISO 3116 alpha-2 code. You can find a list of the codes here.

country=GB
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
 scan_ssid=1
 ssid="MyNetworkSSID"
 psk="Pa55w0rd12345"
}

Once this is done, save the file. You may now eject the SD card safely and plug it into the Raspberry Pi. Power on the Pi. You should see a solid red LED and a flashing green activity LED on the Pi. Allow 2 minutes for the Pi to fully boot for the first time. Find the IP your Pi has taken from your router via DHCP. To do this, you can navigate to your router’s admin control panel and check the list of connected devices. Look for the IP next to the Raspberry Pi. Use this IP to connect via SSH. If you’re Windows, we recommend using PuTTY as your SSH terminal. On Mac/Linux, terminal is fine (ssh pi@IPADDRESS). The default username and password is “pi” and “raspberry”.

Measure Temperature and Humidity with DHT11/DHT22 Sensor

With very little knowledge and effort, it is easy to measure the temperature and humidity using a sensor. Sensors such as the DHT11 and DHT22 are affordable, accurate and can measure both temperature and humidity.

Equipment List

To make this tutorial easier, we will be using a DHT11/DHT22 with PCB. Therefore there is no need to use a 10kΩ resistor.

You can see the difference between the DHT11 and DHT22 here. In short, the DHT11 is cheaper, but the DHT22 is more precise and lasts longer. For applications in areas where accuracy and reliability is more important, the DHT22 should be used. Personally, we think the DHT22 should be used wherever possible as its only slightly more expensive.

Setting up the Hardware


The left pin of the sensor is connected to 3V3 of Pi (pin1), the second pin on the sensor is connected to a free GPIO pin on the raspberry (GPIO26, pin37) and the right pin of the sensor goes to GND (Pin9) on the Pi. The setup is identical for both DHT11 and DHT22 since the pins are assigned the same way.

Getting Readings from the Sensor on the Raspberry Pi

Before continuing, the below steps assume your Pi has Raspbian installed and is connected to the internet. If you haven’t done this yet, please follow the steps here.

We need to install some packages:

sudo apt-get update 
sudo apt-get install build-essential python-dev python-openssl git

Now we need the library for the sensors. We use a pre-built Adafruit library that supports the DHT11 and DHT22:

cd /home/pi
git clone https://github.com/adafruit/Adafruit_Python_DHT.git && cd Adafruit_Python_DHT 
sudo python setup.py install

This creates the Adafruit Python library that we can easily integrate into our projects. If everything went well, we should be able to read the temperature and humidity. Let’s test our setup with the example script provided by Adafruit. The first parameter (22) indicates which sensor we are using (11 if you are using the DHT11) and the second is which GPIO it is connected to (not the pin number, but the GPIO number). Run these commands:

cd examples
sudo ./AdafruitDHT.py 22 26

This produces an output like the following:

pi@how2pi:~ $ sudo ./AdafruitDHT.py 22 26
Temp=23.2*  Humidity=45.1%

Be aware: Both DHT11 and DHT22 can only read at a rate of every 2 seconds. We recommend a minimum interval of 3-5 seconds.

In order to utilise the temperature and humidity library into other Python scripts, you only need the following code:

import Adafruit_DHT
...
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
...