Category: Raspberry PI

Relay module boards play an important role in Home automation projects. Generally we connect the load ( fan,light,etc…) to be controlled through the Relay contacts . While is the relay is controlled ON/OFF , IN TURN the load is controlled through the relay contacts.

Different types of relay modules are available .

In this post we explore the best Relay module suitable to use with Raspberry PI.

Relay modules are available in 12v & 5v .The best one for RPI is 5V Relay Module with Opto coupler drive.

This type of relay boards have opto coupler & NPN transistor to control the relays. Each relay is controlled by an opto+transistor combInation.

When a LOW is applied to INPUT , say IN1 , the photo diode inside optocoupler conducts which in turn activates the photo transistor inside the opto.

The NPN TRANSISTOR after the opto coupler is triggered which in turn switches ON the relay.

On board a Jumper is provided between JD_VCC & Vcc .While the jumper is ON & Vcc is provided with 5v , the board operates normally. When LOW is at input , the relay is ON. The problem arises when connected to RPI whose GPIO pins are at 3.3v level.

When a HIGH is applied from RPI , THERE WILL BE POTENTIAL DIFFERENCE OF 5V – 3.3V = 1.7V which causes the board relay to chatter or malfunction.

So while using with Raspberry PI always remove the jumper & provide 5V to JD_VCC & 3.3V to Vcc.

Both these voltages can be directly connected from the RPI itself. No separate power source required for Relay board. In RPI 3.3V is capable of only 50 milli amp sourcing current.

This 3.3v is connected to Vcc of Relay board which drives only the photo diode of opto coupler.So a small current is enough.

The 5V rail of RPI is capable of sourcing much more current. It depends on the power adapter used for RPI less the current consumed by the RPI itself . 500MilliAmps can be easily sourced at this 5v point of RPI.

By default, Raspbian Stretch version uses Python 2. However, versions 2 and 3 come installed by default. We just have to make 1 minor change so that the Pi uses Python 3 whenever we type python into a terminal.

In a terminal window, enter the following command:

nano ~/.bashrc

Scroll down to the bottom, and add the following command to the file:

alias python=’/usr/bin/python3′

Exit out of nano by pressing ctrl+x, press the y key when prompted if you want to save the file, and press the enter key.

you can run the contents of the .bashrc script by entering:

source ~/.bashrc

Now check the version of Python

python –version

Just type in python to get the INTERPRETER symbol >>>

Now you can test python commands one by one.

First import RPi.GPIO module

import RPi.GPIO as GPIO

Functions are called Modules in Python. Note the casing of letters used RP in capitals & i in small case .We also use alias name GPIO , so that further in code you just need to mention GPIO instad of RPi.GPIO

To specify which pin configuration you are using type

GPIO.setmode(GPIO.BOARD)

There are two ways of numbering the IO pins on a Raspberry Pi within RPi.GPIO. The first is using the BOARD numbering system. This refers to the pin numbers on the P1 header of the Raspberry Pi board. The advantage of using this numbering system is that your hardware will always work, regardless of the board revision of the RPi. You will not need to rewire your connector or change your code.

The second numbering system is the BCM numbers. This is a lower level way of working – it refers to the channel numbers on the Broadcom SOC. You have to always work with a diagram of which channel number goes to which pin on the RPi board. Your script could break between revisions of Raspberry Pi boards.

To detect which pin numbering system has been set (for example, by another Python module):

mode = GPIO.getmode()

The mode will be GPIO.BOARD, GPIO.BCM or None

Physical pin 32 is set as OUTPUT using

GPIO.setup(32,GPIO.OUT)

Pin 32 is initially made HIGH so that relay is OFF

GPIO.output(32,GPIO.HIGH)

To make the Relay ON , set pin 32 to LOW

GPIO.output(32,GPIO.LOW)

At the end any program, it is good practice to clean up any resources you might have used.

To clean up at the end of your script:

GPIO.cleanup()

Note that GPIO.cleanup() also clears the pin numbering system in use.

Now let us connect other Relay inputs to RPI

PIN 36 to IN2

PIN 38 to IN3

PIN 40 toIN4

Open NANO editor by typing

sudo nano relay.py

Feed in the following code.

Note the usage of Indentation for try block.

You can press TAB key to introduce blank space indentation whichis equal to braces in PYTHON.

At times , the PI is not recognized , or unable to contact over SSH in this method.

Let us see the DIRECT NETWORK LINK method In this post which is 100% reliable.Cons is that network internet is not shared.But this is not an issue , as PI3 is WIFI capable through which you can connect to internet.

To start with , place the SD card ( class 10 type recommended ) with Raspbian OS on to a Card Reader

The card will be shown as boot drive.Open it.

Under View , un tick the option “ File Name Extension”

Now Right click on empty space & create a new text document.

Rename it to ssh & save it.

Now a file named ssh is created without any file extension

When the card is now used on RPI, on first boot the OS will look out for the ssh file name.If it finds one , it enables secure shell & then delete the file named ssh.

This is how SSH is enabled in a Headless mode operation of RPI.

Place the SD card on to PI & power it up.

Now Open Network& Sharing center on your Windows PC.

Connect the Ethernet port of RPI to the Ethernet port of your PC using a STRAIGHT RJ45 Cable.

An Unidentified Network is created for the Ethernet adapter where PI is connected .

Click on the Network connection type ,( here mine is WIFI.)

Click properties

Under SHARING Tab un tick the options so that Network sharing is DISABLED.

7 Inch LCDs work straight from the box as soon as you plug it onto your PI.No driver is required , but the touch response is poor & screen width is not full.

This post walks you through the steps of installing proper Drivers for touch to work smoothly & also installation of virtual keyboard is introduced.

The 7 inch LCD used here has a resolution of 800 x 480 pixels & the screen is Capacitive Touch.

HDMI interface is provided for Display & USB interface for touch control. Both the HDMI & USB cables are provided in the box.

Connect the HDMI cable to the HDMI port of Raspberry PI & THE USB cable to any one port of the RPI.Slide the mini switch to ON position.

Provide a 5v 2Amp power source to the RPI , no separate power is required for the LCD

Connect the RJ45 cable from the Internet providing Router to the RJ45 jack of Raspberry PI.In case of PI3 also this internet connection is a must for driver download & setup.Later you can switch on the WIFI on PI3 to connect with internet.

Insert the micro SD card with the latest RPI3 RASPBIAN Image on to the PI & switch ON the power.

You can see the initial screen is not Full screen. But interestingly , the touch will work.

step 1 : CONFIG.txt editing to get FULL Screen Display :

To get the full screen display , you need to edit the CONFI.TXT file inside /boot folder.You can remove the SD card & plug it to PC using a card reader.

Open the folder & look out for the CONFIG.TXT file .Open this file using Wordpad or Notepad++.

Scroll down to the end of the file & append these lines :

Setting max_usb_current=1 sets the available current over USB to 1.2A (default is 600mA)
This can help if you have a decent power supply (2A, at least) and need to power the external Touch screen display.

For supporting HDMI modes that aren’t from the official CEA/DMT list of modes
use hdmi_mode=87

In the previous post we’ve seen how to control Raspberry PI from Windows PC using SSH (Secured SHell).The disadvantage in this mode is that SSH is Text only mode (Text User Interface).You can control your PI in Command mode only.Graphical User Interface (GUI) is not possible.

For GUI control we shall use TIGHTVNC server.

TightVNC is a free remote control software package. With TightVNC, you can see the desktop of a remote machine like Raspberry PI and control it with your local mouse and keyboard, just like you would do it sitting in the front of PI.

RASPBERRY PI side settings :

The Raspberry PI should be connected through a RJ45 cable to a Router which is providing Local Network connections .

To start with install the TightVNC server on your PI by typing in the following command inside the LX terminal.

This is a six piece laser cut box for your PI.It has dragon claws to fix and no need of any screws or bolts.It is a must to protect your PI .If you accidentally place your PI on any conductive surface ,PI may go defective beyond repairable.

So make your PI safe with this little cute classic box.

The case comes as 6 pieces that snap together, made of crystal-clear acrylic. You can use all of the connectors on the edges of the Pi: HDMI, Audio, Video, SD slot, micro USB power, Ethernet and the two USB ports.

The enclosure is designed so that you can remove the top piece and plug any sort of cables you wish into the breakouts in the middle.

SSH is a secure method of logging onto a remote computer. If your Pi is networked then this can be a handy way of operating it from another computer or just copying files to or from it.

SSH is Secured SHell .

First you have to install the SSH service.

The wheezy Raspbian OS comes with SSH telnet access enabled, and this is a great way to get terminal access to your Raspberry Pi over network ,without having to hook it up to a monitor and keyboard.

All you need is to connect your Raspberry Pi to power and an internet connection.

Open the LXTerminal & type in sudo raspi-config

The list of set up options is displayed.Move down to select Advanced Options.

Under Advanced Options window select SSH.

Enable the SSH service

Click on OK & then FINISH.

Reboot your PI.

To connect PI over network you should know the IP address of PI.

You can type in either of the following command to learn the address.

ifconfig

or

ip addr show

By default the Raspberry Pi will be allocated an IP address by your router. However, next time you power it up the router may allocate a different IP address. This is a problem because in order to connect through SSH we need to know the Raspberry Pi’s IP address on your local network, and we can’t see this if we don’t have a monitor attached.

There is a a great free tool called Advanced IP Scanner to find out the IP address.

The Raspberry Pi is a credit-card sized computer that plugs into your TV . It’s a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video.A keyboard & a mouse can be connected to the dual USB port (type B) .

Raspberry PI comes in 2 models. Model A & Model B.

Model A has 256MB RAM, one USB port .There is no Ethernet on the Model A version

Model B has 512MB RAM, 2 USB ports and an Ethernet port.The Model B version of the device includes 10/100 wired Ethernet. But Wi-Fi will be available via a standard USB dongle.

Both models have HDMI connectivity. Beyond this, mice, keyboards, network adapters and external storage will all connect via a USB hub.You can also attach a USB stick or USB hard drive for storage.

The Raspberry Pi has a Broadcom BCM2835 SoC (System On Chip), which includes an ARM11 700 MHz processor (ARM1176JZF-S) & and a Videocore 4 GPU. The GPU is capable of BluRay quality playback, using H.264 at 40MBits/s. It has a fast 3D core accessed using the supplied OpenGL ES2.0 and OpenVG libraries.

The RAM is a POP package on top of the SoC, so it’s not removable or swappable.You cannot add extra RAM.

It does not include a built-in hard drive. An SD card is used for booting and storage.

When you order for a Raspberry PI you will get only the Board.. The power supply & SD card are not included & to be purchased separately.

There is composite as well as HDMI out on the board, so you can hook it up to an old analog TV, to a digital TV . There is no VGA support, but adaptors are available, although these are relatively expensive.There’s a standard 3.5mm jack for audio out. You can add any supported USB microphone for audio in.

The device is powered by 5v micro USB.

The OS is stored on the SD card.There are many Distros available ,but Debian is the default distribution. It’s straightforward to replace the root partition on the SD card with another ARM Linux distro if you want to use something else

By default, Python is supported as the educational language. Java is also supported.

At present Ubuntu can’t commit to support Raspberry Pi .Also Android is not stable enough with PI.

However development work is continuing on these platforms.

Now let us see how to install the OS on to the SD card. SD cards up to 32GB can be used.

We shall make use of 8GB micro SD card.You need an adapter to hold this micro sd card , so that it can match the slide slot of Raspberry PI.

The wheezy-Raspian is a stable OS for Raspberry PI. Recently NOOBS is available ,which is a collection of 5 operating systems.You can download NOOBS & select any one OS to install.As it is a huge download of over 1GB we shall stick on to raw image of wheezy-raspbian.

Remove the SD card from the card reader.Slide it on to the slot at the back of Raspberry PI.Connect an USB Keyboard & mouse to the USB ports at PI.

We can connect the PI to an old TV through the yellow RCA connector of PI. This is the composite video output from Raspberry PI .A RCA cable can be used to connect this pin to the Video IN of your TV.This connector is also yellow in color on your TV (AV IN).

For demo purpose we’re making use of a small 7 inch LCD monitor with AV IN capability.

Now power up the Raspberry PI with 5V /2Amp adapter (micro usb type ).

You can see on the screen , Raspbian booting up.

Following are the one time initial settings to be made.Settings are made through the Keyboard connected to the PI.

First option is “Expand File system “.

Select this to use the full storage area of the SD card.On next reboot this allocation will be done.

Next is the “Change User Password “.If you need to change the user password , select this & proceed as directed.

The third one is the most required one “ Enable Boot to Desktop”.

Select that & press Enter.

On the next screen Select “Desktop log in as user pi at the graphical desktop”.