Latest Entries »


VIDEO Demo :


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.

Let us now connect PI with 4 channel relay board.


JD_VCC              5v

VCC                  3.3V

IN1                   Pin 32

GND                 GND

Ethernet cable is connected between PC & RPI,&  PI is accessed from PC through SSH client PUTTY

Refer my previous blog on how to access PI from PC




LOG IN using default user name pi &password raspberry

To test python commands start the python interpreter by typing command


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


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


Pin 32 is initially made HIGH so that relay is OFF


To make the Relay ON , set pin 32 to 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:


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

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.



import RPi.GPIO as GPIO
import time






# main loop

      print "First Reay ON"
     print "Second Relay ON"
     print "Third Relay ON"
     print "Fourth Relay ON"
    print "Good bye!"

except  KeyboardInterrupt:




Press CTRL+X  to quit & press Y to save the file.

To execute the file


The Relays will be switched ON one by one & finally all Relays will be OFF , according to code.




The single channel relay boards are  different. Some boards have OPTO Coupler & some

do not have opto.

    r1                r2

The board with opto is HIGH enabled. A HIGH is required at IN to switch ON relay.

The board without OPTO is LOW enabled . A LOW is applied at IN to switch ON Relay.

Jumper is not seen on these single relay boards.

Always use 3.3v to Vcc in these single channel relay boards.



This post is on interfacing GSM with Raspberry PI 3  B or B+.

UART is commonly used on the Pi as a convenient way to control it over the GPIO, or access the kernel boot messages from the serial console (enabled by default).

It can also be used as a way to interface GSM / GPS /Arduino / ESP8266, etc with your Pi.

Be careful with logic-levels between the devices , for example the Pi is 3.3v and the GSM is 5v.




In the previous versions of PI GPIO Serial port is mapped to /dev/ttyAMA0

(COM1 equivalent is found on pins 14 and 15 of the GPIO header and is called /dev/ttyAMA0  )

But in PI 3 B or B+ this hardware port has been utilized for the BLUETOOTH Functionality.

/dev/ttyAMA0 is a hardware , high performance serial port (uart) & used for the Bluetooth

A second port is partly software /dev/ttyS0 & is assigned to GPIO Serial port.

So , the ports on a Raspberry Pi 3  are


/dev/ttyAMA0 -> Bluetooth
/dev/ttyS0 -> GPIO serial port   (referred to as the “mini uart”).


Thus on a Raspberry Pi 3 serial0 will point to GPIO pins 14 and 15 and use the “mini-uart” ( /dev/ttyS0).

On older Raspberry Pi’s  it will point to the hardware UART and /dev/ttyAMA0.

Image A


To start with write the  RASPBIAN image OS on a class 10 memory card using ETCHER



Use the SD card on RPI 3 & log in

To know the Serial ports enabled  , type in

cd /dev   & then  ls –l


Image 2

You can see only the ttyAMA0 is enabled & not the  mini uart  ttyS0

Image B

The GPIO serial port is disabled by default. In order to enable it, edit config.txt:

$ sudo nano /boot/config.txt

and add the line (at the bottom):


Reboot for the changes to take effect.

Image 5


Image 7


Image 8

Restart the Putty SSH

Now check the   cd /dev  ,  ls –l    again

You can see both the ports enabled. Note that this can be done through raspi-config also.

Image 3

Let us connect the GSM with PI now.



The logic levels of PI are at 3.3 volts & it is risky to connect directly with a 5v level GSM pin.

The Tx pin (pin 8 , GPIO14) of PI can be directly connected to Rx of GSM. As GSM accepts 3.3volt level as high , this connection is straight without level shifting.

However the Rx pin (pin 10, GPIO 15) of PI cannot be connected directly to Tx of GSM.

A simple level shifter using a signal diode 1N4148 &  a 10k resistor is used in between as shown below



When the level at GSM Tx is LOW , the diode is forward biased & the Rx of PI gets LOW.

When the level at GSM Tx is HIGH ,the diode is Reverse biased & the 3.3v is available at Rx of PI through the resistor.

A separate power source 12v 1 amp is required for GSM , and the GND pins of PI & GSM are made common.

Be cautious while connecting with PI pins , as any wrong connection will render the PI defective.

After powering the GSM wait for the network. The network LED blinks fast initially  & after getting Network it blinks slowly.

You can also call to the SIM number used on the GSM to get a ring back tone.

Now the set upis ready.

Let us install  a Serial monitor called picocom on PI to test the AT commands.


Image 4

Start the picocom with baud rate 9600 & at portttyS0

picocom –baud 9600 /dev/ttyS0

Note the double hyphen before baud .

Image 9

You get Terminal Ready

after which you can type in AT to get an OK response.

If you cannot seewhat you type,or if no response then check the GSM for network &also check the port name you’ve typed.

It must be /dev/ttyS0 .      S is capital letter & then zero.

Now you can make a call using ATDxxxxx; command , terminate a call using ATH

Send SMS using AT+CMGF=1 , & test other AT commands


Image 11


Now let us use PYTHON to make a call.

Open a file named using

sudo nano


Image 12

Type in following code & then CTRL+X , to exit & Yes to save.

Image 13

While using AT command inside Python code , notice the usage of carriage return

\r  after each command    like ATDxxx; \r

To execute the file


You can see the number being called & then hangs up using ATH command.

Image 16

Now let us create a python code for sending SMS.

Notice the usage of  double quotes for sending AT commands

Also, note that single quotes used if the AT command needs to send double quotes

as in AT+CMGS command

Image 14


To execute type in


You can see the sms being sent to the phone number used in code.

Image 17

Support VIDEO :




In my previous post I’ve explained the method  of connecting Raspberry PI with  Laptop through Ethernet.

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


Image 1


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

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


Image 2

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.

Image 3

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

Now Open Network& Sharing center on your Windows PC.

Image 4


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 .


Image 7


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.




Click on the ETHERNET connection & then Properties on next window.

Double click Internet Protocol 4 & ensure that DHCP is enabled i.e, Obtain IP address automatically is selected.

Image 8


For a network link to work between two computers they need to be using the same address range.
The allowable address range is determined by the subnet mask.  means all octets in addresses should be same except for the last two in the IP address,otherwise they will be filtered.

Most PCs connected directly to another computer will allocate an IP address in the range 169.254.x.x ( with subnet mask of

For RPI to be able to communicate through the direct link it needs to have a fixed IP address in the same address range 169.254.x.x

We can set this in the cmdline.txt file

To know the IP allotted to the ETHERNET adapter

Open Command Prompt ,CMD

Type in ipconfig /all

Image 9

Scroll down to find the IP under Ethernet adapter.

Here it is

which is the Gateway IP of Ethernet adapter.Any device connected to this port should have the IP in range 169.254.192.x

As we’ve connected the PI to Ethernet adapter  let us assign the IP toPI as

Note that the last octet can be anything up to 255.

Image 10

The static IP to PI is assigned as follows :

Switch off power to PI ,remove SD card & place it on a card reader.

Open cmdline file found in the SD card

Image 11

Move to the End of file.

Please note that you must not hit Enter .

Just at the end type in


Image 12


Image 13

Save the file .Remove SD card from card reader & plug it to PI & power it up.

Let us use the free SSH Client PUTTY  to log intoPI.

Open PUTTY &  under Session , type in the IP address

Ensure that SSH is selected.


Image 14


Click open

Now you get the LOG IN window of PI

login as pi & password : raspberry


Image 15

Now ,let us see how to log in Graphical user interface.

For this a XMING X Server is required.

Download XMING from here

Install XMING & start the server.

Image 16


Image 19

Open Putty & as done previously enter the IP address

Under SSH click X11

& then Tick mark Enable X11 forwarding.


Image 17

Click Open to get the login window of PI.

To get Graphical interface , type in


Image 18

Now you get the graphical screen

To get internet access :

Under  SYSTEM TOOLS  click LXTerminal

Image 20

Type in

sudo raspi-config

Image 21


Select Network Options

Image 22

Select WI FI

Image 23

Then enter  SSID of your WIFI to connect with


Image 24


Provide the password for thee SSID

Image 25



Image 26

Now you get the internet accesswhich can be tested by

sudo apt-get update.


Image 27





Installing USBASP drivers on Windows 8 or 10 is bit tricky. As these versions of Windows require DEVICE DRIVER VERIFICATION , they do not allow unsigned drivers like libusb.

For programming 8051 or AVR you need this driver.

You can install by following methods :

1. Disabling Windows driver enforcement on boot up. Follow this link

This method requires SHIFT+ RESTART of Windows to enter advanced troubleshooting where we disable signature verification .After this you can install the libusb driver.

But in some laptops , this method fails on next Windows update.


2. Install libusb drivers using ZADIG application.

Zadig is meant to install a libusb compatible driver, for a device that does not yet have such a driver installed.

With the application running, you should see your USB device appear in the dropdown list

(click on the dropdown to see all devices).
You can also plug your USB device after Zadig is running, as the list is refreshed automatically. Its recommended to leave only the device you want to install a driver for, and unplug any other USB device.


You can also select the driver you want to install,( between WinUSB/ libusb-win32/libusb0 / libusbK )

Once you are satisfied with the selection and device description, simply click the Install Driver Button.

Caution :

If you select the wrong device,  Zadig will  replace its driver.

The responsibility is entirely yours try to pay attention to the device you select .Don’t just  press the install button on the first device you see,

If wrong driver is replaced , then some of your USB devices like wireless mouse may not work.


3. Best & Easy method

The USBASP windows driver was previously based on  libusb-win32. The certificate on the base drivers have expired and the library has now been superseded by libusbK.


Following is the procedure to install protostack USBASP driver.

At first when you connect the USBASP hardware to PC & open the Device Manager you see an exclamation mark against USBASP indicating that drivers not installed.


Image 2

If you open the PROGISP application ( to load HEX file on to8051) ,you see the PRG ISP icon greyed out ,as the programmer is not yet detected.

Image 1


Download the new USBASP driver from

This driver should work with version of Windows XP right through to 8.1 and the version 10 . (both 32 and 64 bit editions).

Because the driver is signed, there should be no need to disable driver certificate enforcement or use Zadig.

Unzip the downloaded folder.

Click on Install Driver application

Image 4


Image 5


Image 6

Once the driver is installed , under Device Manager you can see USABASP under

libusbK Usb devices

Image 7

Now ,open the PROGISP application to see the device ready.

Image 8


SIM808 is all in one module with GSM , GPRS,GPS & BLUETOOTH.SIM808 is advanced compared to SIM908.

SIM808 has GNSS receiver which enables easy GPS FIX.

In this post we explore how to parse GPS data ( without using TNYGPS library) & send it through inbuilt GSM

SIM 808 has 2 antenna sockets one  for GSM  & other for GPS. A stub antenna is used for GSM & a magnetic external patch antenna is used for GPS.

Do not interchange these 2 antennae. The antenna socket nearest to SIM808 chip is for GPS.



Connection between Arduino UNO & SIM808 is simple.

Gnd to Gnd

Tx of SIM808 to Rx of Arduino (soft serial digital pin 7)

Rx of SIM808 toTx of Arduino (soft serial digital pin 8)

A separate power adapter of 12v 2amp is required for SIM808 board.

Insert valid SIM on to the slot at the  back of SIM808 board

Power on & notice the NETWORK LED .It blinks fast initially & when it gets network it blinks slowly ,once per second.




We’ll use a serial to usb cable to monitor SIM808 serial port. We can also test AT commands through this port ,before uploading Arduino code.

Connect the cable to PC & note the COM port allotted under Device Manager .

Another COM port is allotted to Arduino board.Note this also.


Before uploading code on to Arduino , let us check the AT commands.

For this open a PUTTY Terminal & select SERIAL.

Feed in the COM port of the USB-RS232 cable &baud rate as 9600. & click open.

Image 1


Type in AT ,the SIM808 module responds with OK.

ATD is the command to dial a number

AT+CMGF=1 , AT+CMGS=”phone no.” are for sending SMS.

All GSM related commands are similar to that of SIM800 or SIM900.

Some cases of empty SMS delivery is reported .i.e you receive SMS on mobile but has got no message.

To solve this problem set the following parameter.


This has to be done before you set the module to Text mode using AT_CMGF=1

Image 2

GPS related AT Commands are similar to that of SIM 908.



AT+CGPSPWR=1 sets the GPS engine ON


AT+CGSINF=0 returns a single NMEA sentence of GPS.

AT+CGPSSTATUS?   returns the Status of GPS whether it has got FIX or not.

If GPS has not received FIX , the NMEA sentence will read all  0 s

Relocate the GPS antenna & wait for some time to get a FIX. If the GPS antenna faces open SKY you get a FIX within seconds.

Image 3

Once GPS gets a FIX , AT+CGPSINF=0 returns a valid NMEA sentence.

AT+CGPSINF=32  returns a single GPRMC sentence


Image 4


Now let us see the SIM808 specific GPS commands

SIM808 has inbuilt GNSS receiver

AT+CGNSPWR=1  turns on the GNSS power

AT+CGNSSEQ=”RMC”  parses the NMEA sentence related to GPRMC

Now issuing the command AT_CGNSINF returns a valid NMEA  GPRMC sentence



Image 5


Now let us upload the code that parses RMC data &sends SMS with google maps link


Arduino code Download :



#include <SoftwareSerial.h>
SoftwareSerial sim808(7,8);

char phone_no[] = “xxxxxxx”; // replace with your phone no.
String data[5];
#define DEBUG true
String state,timegps,latitude,longitude;

void setup() {

sim808.print(“AT+CSMP=17,167,0,0”);  // set this parameter if empty SMS received



void loop() {
if (state !=0) {
Serial.println(“State  :”+state);
Serial.println(“Time  :”+timegps);
Serial.println(“Latitude  :”+latitude);
Serial.println(“Longitude  :”+longitude);



sim808.print (longitude);
sim808.println((char)26); // End AT command with a ^Z, ASCII code 26

} else {
Serial.println(“GPS Initializing…”);

void sendTabData(String command , const int timeout , boolean debug){

long int time = millis();
int i = 0;

while((time+timeout) > millis()){
char c =;
if (c != ‘,’) {
data[i] +=c;
} else {
if (i == 5) {
goto exitL;
if (debug) {
state = data[1];
timegps = data[2];
latitude = data[3];
longitude =data[4];
String sendData (String command , const int timeout ,boolean debug){
String response = “”;
long int time = millis();
int i = 0;

while ( (time+timeout ) > millis()){
while (sim808.available()){
char c =;
response +=c;
if (debug) {
return response;


Following is the serial port monitor screenshot.

Once the command AT+CGNSINF is issued ,it returns a valid RMC sentence.

The latitude,longitude is then parsed

SMS is then sent with parsed data appended to google maps link.

Image 7


Pleae note Code is written to send SMS every 20 secs.







In this post let us explore the GPS Module built on UBLOX Neo series & then parse the data to send SMS with google maps link.

Arduino UNO is used to control the GPS module.

The GPS module used here has provision for connecting an external magnetic patch antenna.This enable the GPS to get FIX  within seconds even while testing indoor.

To test the GPS we need an USB to TTL  module.Or we can use our Arduino itself with a small hack.

Connect the RST pin of Arduino to GND.This will bypass the bootloader of Arduino & we can use the serial IC of Arduino to communicate with GPS.

Connect Tx of GPS to TX of Arduino & Gnd toGnd.

Note that the connection is STRAIGHT here & not reverse .Remember this connection will be reverse while we actually upload code to Arduino & use it in regular mode.

We can power the GPS module from Arduino itself. Connect 5v of Arduino to 5v of GPS.

Wait for some time for the GPS to get FIX. Once a FIX is received a green LED flashes on the GPS board indicating that GPS is ready with data flow.


Connect the USB of Arduinot o PC & open the DEVICE MANAGER to note the COM port allotted.

Open the Arduino IDE & select the PORT allotted.

Image 8


Image 9

Open the SERIALMONITOR to see the FLOW of NMEA sentences

We’re interested  in GPRMC which is the recommended minimum data for gps

There is an interesting link where we can decode the GPRMC sentence & understand the contents

Copy & paste a GPRMC sentence in this site to decode the contents.



If you notice a V  in GPRMC sentence ,it means GPS has not yet got a FIX. Relocate the antenna & wait for some time to receive a FIX & V changes to A .

Now the RMC sentence has all  the  data latitude,longitude,time,speed,altitude,etc..

Image 4



Image 10


Copy a portion of the data flow & paste it on to a Notepad.

Save it as text file with a name.


Image 11


To visualize the data , go to this link

Click on Choose file & select the notepad file saved.

Select the Output Format as GOOGLE MAPS  & click MAPIT.

Image 12

You can see the location on Map.

Image 13


Now your GPS is working.Let us go for a practical demo with Arduino.

To parse the data there are libraries that make life easier.

One such library is TINYGPS++ from Arduiniana

Download the library ,unzip it.

Image 14

Rename it to TinyGPSPlus & copy paste to Arduino –> Libraries.

Image 15

Open the Arduino IDE to see a list of examples against TinyGPS++

Select the FULLEXAMPLE  code.

Image 16

Here Softwareserial is used & digital pins 4 is defined as RX , 3 as Tx

Connect the Tx of GPS to  Rx of Arduino (digital pin 4)

Gnd toGnd.

As we only receive data from GPS , a single connection is enough. Tx of GPS toRx of Arduino

Image 17


Now upload the code & open the Serial monitor on baud 115200.

No data is received

Image 19

This is because of BAUD RATE mismatch.

Change the GPS Baud (not the serial baud)  to 9600 & then upload.


Image 20


Now you get data which is displayed neatly on serial monitor.

Image 21

In the next example we shall parse the GPS data & send SMS as google maps link using a GSM.

For this we use the previous version of library TINYGPS .


Image 22

Download the library & install it.

Here we use SIM900 GSM .A separate power source of 12v 1amp is required for GSM.

Connection between Arduino & GPS :


Tx of GPS connected to Rx of Arduino


Gnd to Gnd

5v of Arduino to 5v of GPS.


Connection between Arduino & GSM


Gnd to Gnd

Tx of GSM to Rx (soft serial pin 7) of Arduino

Rx of GSM to Tx (soft serial pin 8) of Arduino




Download the code from here


In this code we make use of older version of TINYGPS library to parse the latitude,longitude data,


Image 23

The data parsed is then appended to google maps link&sent as SMS .

Every 20 seconds an SMS is sent.You can modify the code so that you get SMS only when you call to the GSM.


Image 24


Upload the code & get the SMS on themobile no. you provided in the code.

Touch on the SMS link to see the location of GPS.


IMG_20180421_170826         IMG_20180421_170829






ESP8266 WEMOS D1 With I2C Serial LCD

WemosD1 R1 board is now  the most preferred IOT WIFI Board .As the hardware resembles Arduino , it is most sought out  board for any IOT project , than NODEMCU.

To add a display I prefer to use the SERIAL LCD WITH I2C BACKPACK  .

The serial LCD is built on PCF8574 &  works on I2C Bus ,thus requires only 2 wires SCL & SDA apart from the power pins.

The LCD works on 5V & it can be sourced from D1 board itself.



For more detailed information on this Serial LCD you can read my previous blog here :


The connection between D1 board & LCD is simple .

Wemos D1      LCD

5v           —–  Vcc

Gnd        —- Gnd

SCL  D15  (GPIO5)  —    SCL

SDA  D14   (GPIO4)   — SDA

To start with download the latest Library from this link :

From Arduino IDE  Sketch –> Include library –> Add ZIP Library

Browse to the location of the downloaded ZIP file & select it

Image 2

Now the Library for Serial LCD is installed.

Image 3


Under Tools selectthe board as WEMOS D1 R1 , upload speed as 115200, port is the one allotted  in Device manager &programmer USBASP.

Image 4

Read this previous blog post of getting started with D1 board, on how to install Arduino core using Boards Manager


Now we need to know the I2C Address of the LCD board connected to D1.

For this upload the following Scanner sketch.


Image 5


Once uploaded,Open the serial monitor with 9600 baud setting.

The I2C HEX Address of the device is displayed .Here it is 0x27 .

Note that this address varies according to the manufacturer and the jumper solder on A0 A1 A2 pins on the backpack pcb.


Image 6

Now open the first example lcd_test.

In this example 2 header files are used

One is the Wire.h ,which is the I2C library that is built in already with IDE.

Second  is the LiquidCrystal_I2C.h which we installed initially.

Then we create an instance  lcd with parameters I2C Address  , EN , RW , RS , D4,D5,D6,D7

I2C address is the ine we got from the scanner code –0x27

EN,RW,RS are the pins of I2C board connected to LCD Enable ,Read/Write & Register Select respectively.

D4 to D7 are Data pins


Image 3

lcd,begin(16,2) initializes the 16 x 2 LCD

Then we set the backlight ON for the LCD

lcd.home()moves the cursor to Home position

lcd.print(“,,,,”) prints the message on 1st row.

lcd.setCursor(0,1) moves the cursor to second line.


Image 7


Upload the code to see the result on LCD.

The Contrast to LCD can be adjusted by the preset on the back.




Here is the second example which prints the message typed on the Serial monitor.


Image 8


Image 9



WEMOS D1 is a WIFI development board based on ESP8266 12E. The functioning is similar to that of NODEMCU , except that the hardware is built resembling Arduino UNO.

The D1 board can be configured to work on Arduino environment using BOARDS MANAGER.

So,its not necessary to use a separate Arduino board , the D1 itself can be programmed on Arduino IDE.This is handy in using with IOT projects. Further many Arduino SHIELDs are compatible with the D1 board.

Different versions of D1 boards are available in the market    R1  ,  R2  , MINI

As the name implies D1 Mini is a smaller version

R1 , R2 boards look like Arduino UNO board ,but version is not printed on board. On some boards WEMOS D1is printed , as the ESP12 chip is manufactured by WEMOS.

In some other boards it is printed just D1 ,where the ESP12 chip is that of AI Thinker.

As the ESP12 chip has only one ANALOG pin , on board there is only A0 & other analog pins are dummy .

Image 2

Image 3   Image 4

Use a MicroUSB cable to connect D1 with PC. Make sure that the cable is capable of handling both Data & power (some of the mobile cables can handle  only power & not data) .

Open the DEVICE MANAGER & note the COM PORT allotted to the module.

As the serial chip used on D1 is CH340 , you need to install the corresponding drivers.

Image 1



Following is the pin configuration of R2 typeD1 Module.Note the changes in GPIO Pin position.


Open the Arduino IDE &  select this COM port.

From TOOLS  —>  BOARDS  —> BOARDS MANAGER install the package for ESP8266.

Prior to this , you need to provide the link from where the Boards manager will look for the package.

To do this copy the following Link

Go to FILE  –> PREFERENCES on Arduino IDE

Paste the link against the Additional Boards Manager URLs ,seen at the bottom of preferences screen & click OK.

Image 1.1



Search for ESP8266

Now you will find the package from ESP8266 Community.

Select the latest version from drop down & install it.

It takes a while & requires internet connection.

Image 2.1

Once the package is installed ,you can see the list of  boards under TOOLS –> BOARDS

Select the board as WEMOS D1 R1.

Select UPLOAD speed as 115200 &Programmer as USBASP

Image 3.1

Now open the  File –> Examples –> Basics –> BLINK sketch

No need to modify the sketch ,as we’re going to blink the built in LED (on the ESP chip , there is another LED on board which is mapped to pin14)

Click on UPLOAD


On some PCs you get a compile error ,stating that the file is being used by another process.

On some forums ,it is mentioned that the board is Duplicate .Its not the case, the reason is that some Antivirus program like McAfee does not allow the compilation process.

Open the Antivirus dashboard & TURN OFF the REAL TIME SCANNING for some time



You can also configure the Antivirus settings for an EXCEPTION to Arduino programs.

Now Upload the sketch.


Once the code is uploaded, you can see the built in LED blinking on the chip

Next code is a WIFI scanner which scans for nearby SSIDs & display the names of WIFInetworks with RSSI signal power &encryption mode.

For this we include the Header file ESP8266WiFi.h

This is installed along with the Boards Manager & is a very powerful library.

You can get more information on this library from here :

Many useful Functions are there for this WiFi library.

WiFi.mode (WIFI_STA); 

The mode function of library is used to set the D1 board in STATION mode.


scanNetworks function scans for nearby SSIDs & the result is an integer (number of surrounding WiFis) which is stored in a variable n

Then we print the SSID name,RSSI signal strength ( the lower the negative dB the stronger the signal), & Encryption mode (a * indicates SSID is password protected)


// D1 ESP8266 WiFi scanner

# include  “ESP8266WiFi.h”

void  setup () {

Serial . begin ( 9600 );
// set WiFi to station mode and disconnect from previous connection
WiFi.mode (WIFI_STA) ;
WiFi.disconnect ();
delay ( 100 );

void  loop () {
Serial . println ( “Start Scanning ..” );
// scan for nearby WiFi SSIDs & save their number into n
int n = WiFi.scanNetworks ();
// in case no Network found , print info
if (n == 0 ) {
Serial . println ( “No SSIDs nearby seen…” );

else   {
Serial . print (n);
Serial . println ( “WiFi network SSIDs nearby ….” );
// list all WiFi networks nearby
// name, signal strength and security method
for ( int i = 0 ; i <n; ++ i)
Serial . print (i + 1 );
Serial . print ( “:” );
Serial . print ( WiFi.SSID(i));
Serial . print ( “(” );
Serial .print ( WiFi.RSSI(i));
Serial . print ( “)” );
Serial . println (( WiFi.encryptionType (i) == ENC_TYPE_NONE)?”” : “*” );
delay ( 10 );
// Exit
Serial . println ( “” );
// pause for 15 seconds before new scan
delay ( 15000 );




Support VIDEO:



GSM controlled RELAY with Arduino

In this post we ‘ll control a relay through GSM by sending message.

Parts required  :

GSM SIM900 or SIM800

Relay board  (12v board with active HIGH , i.e if you send HIGH from Arduino then Rlay will be ON)

Arduino UNO

DC connector cable & 12v adapter

We’ll make it simple   By sending SMS $a1 Relay is made ON

BY sending SMS $a0 the Relay is switched OFF

GSM SIM900 board is used in this demo , but you can use SIM800 also. A valid SIM to be inserted in to the slot on GSM board. Do not use 4G SIM like JIO , as the SIM 800 / 900 is 2G /3G enabled.



A 1 channel relay board is used which is a 12v relay & needs a HIGH to enable the relay. Note that if you use a relay board with opto coupler , it needs a LOW to switch ON the relay .In this demo we use a 12v , HIGH enable relay which uses a simple NPN transistor to switch ON the relay.



i.e from Arduino Digital pin you send a HIGH to switch ON the relay.



Digital pin 8 of Arduino is connected to Relay board INPUT

Rx of Arduino to Tx of GSM , Tx of Arduino to Rx of GSM

GND of Arduino to GND of GSM

12V Adapter is used as power source. Power socket is provided on GSM & Arduino boards . DC connectors are used to power up GSM & Arduino .As Arduino has 5v regulator on board you can safely use 12v at power socket.

The +ve pin of Relay board is looped with +12v of adapter & -ve to GND of Relay board.

Ensure that all GND pins are made common , that of GSM , Arduino & Relay boards.




Following is the Arduino code to be uploaded. Remember to disconnect wires at Rx/Tx while uploading code to Arduino.

You can Download the Code HERE



char inchar; // variable to store the incoming character

int Relay = 8;


void setup()


pinMode(Relay, OUTPUT);

digitalWrite(Relay, LOW);



Serial.println(“AT+CMGF=1”); // set mode to text



// just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out




void loop()


//If a character comes in from the GSM…

if(Serial.available() >0)


if (inchar==’$’)



if (inchar==’a’)



if (inchar==’0′)


digitalWrite(Relay, LOW);

Serial.println(“Relay OFF”);


else if (inchar==’1′)


Serial.println(“Relay ON”);

digitalWrite(Relay, HIGH);



Serial.println(“AT+CMGD=1,4”); // delete all SMS







A char variable inchar is used to store the incoming SMS

The relay pin is defined as DIGITAL pin 8

Inside setup function , the Relay pin is declared as OUTPUT using pinMode & then initially it is made LOW

Serial.begin(9600) ; starts Serial communication at baud 9600.

AT+CMGF=1 confirms TEXT mode operation of GSM .Notice the use of SerialPrintln which provides a Carriage Return


// just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out

The AT command +CNMI is used to specify how newly arrived SMS messages should be handled. You can tell the GSM/GPRS modem or mobile phone either to forward newly arrived SMS messages directly to the PC, or to save them in message storage and then notify the PC about their locations in message storage.

Syntax: AT+CNMI=<mode>,<mt>,<bm>,<ds>,<bfr>


This command selects the procedure for message reception from the network.


<mode>: controls the processing of unsolicited result codes

Note: Only <mode>=2 is supported.

<mt>: sets the result code indication routing for SMS-DELIVERs. Default is 0.

0: No SMS-DELIVER indications are routed.

1: SMS-DELIVERs are routed using unsolicited code: +CMTI: “SM”,<index>

2: SMS-DELIVERs (except class 2 messages) are routed using unsolicited code: +CMT:…….

AT+CNMI is used anyway just to get a notification when a SMS arrives, in order to read the content of the stored SMS you have to use AT+CMGL= or AT+CMGR=…


Inside Loop

If any serial data is available it is assigned to inchar variable

If the first character is $ , the inchar variable is replaced with next Serial character

If the next character is a , again the inchar is replaced with next serial character

Finally , if the character is 1 , the pin 8 is made HIGH & relay is made ON

If the final character is 0 , pin 8 is made LOW , thus making the relay OFF.

SMS $a1 makes the Relay ON

SMS $a0 makes the Relay OFF

Only basic code is explained in this post to just ON or OFF the relay. Further improvements can be done like :

1. Relay status sent back as SMS to the caller

2. Programming can be done so that SMS from particular Master number only is considered

& SMS from other number will not control the relay

3. Pump starter can be made ON / OFF by using 2 relays .One relay contact used to START the motor & other to STOP .While connected with STARTER we need to momentarily switch ON / OFF the relays. This is similar to manually pressing the START or STOP button of the motor starter.

Support video :


The  PIC 16F877A , AVR ATMEGA 16,32 , ATMEL 89S51 , 52 , ICs used in Arduino uno,mega all are 8 bit microcontrollers.

LPC2148 is more powerful 32 bit uC. It is ARM 7 controller

  • On-chip flash programmable memory is 512kb
  • It is a high speed controller 60 MHz operation
  • Two 10 bit ADC converters provide a total of 14 analog inputs
  • One 10- bit D/A converter
  • Two 32 bit timers/counters
  • 4- CCM (Capture Compare Modulation), 6-PWM, Watchdog timer
  • One RTC, 9 interrupts
  • One I2C protocol, SPI protocols, SSP protocol
  • Two UART serial communication protocols

For development we use KEIL environment.

We will use evaluation version of MDK-ARM for C programming. This software is available at

Install C51 KEIL & then the MDK-ARM

Image 1

For LPC2148 support you must install the MDK Legacy support files available at

If this support is not installed , LPC2148 will not show up on the KEIL IC list

Image 2

Once the installation of KEIL is completed , open up KEIL

Project –> New uVision Project


Create a Folder for your project & provide a Filename & save.


In the next window dropdown select Legacy Device & search for LPC2148.

Select the IC LPC2148 .

If LPC is not listed then check the legacy support installation


Click on Yes to add STARTUP file to project.

Different manufacturers for LPC2148 use same ARM CORE. So STARTUP file is required which initializes ARM Core Peripherals , define stack program , Interrupt vectors ,etc..

START UP file will be in ASSEMBLY language only & must be part of your program which takes care of HARDWARE INITIALIZATION.


Now the PROJECT is created


Right click on SOURCE GROUP 1 & click “Add New item to Group Source Group 1”


Select C File & provide a name to your C file (without .c extension .Its added automatically)




Image 8

Right click on TARGET1 & select OPTIONS FOR TARGET TARGET 1

Image 9

3 important settings to be done , before starting your C code.

Image 10

The Options for TARGET can also be accessed from  FLASH  –>  CONFIGURE FLASH TOOLS

Image 14

Following 3 settings to be done for proper compiing.

Under TARGET tab select “Use MICROLIB”

Image 11


Image 12


Image 13

If the LINKER setting is not done , your C code will compile to HEX file .But on uploading you will not see any result.

Now click on your  .c file under Source Group & type in the following code :


#include <lpc214x.h>

void delay_ms(unsigned int count)
unsigned int i=0,j=0;

/* start the main program */
int main()
PINSEL1 = 0x000000;  //Configure the P1 Pins for GPIO;
IODIR 0= (1<<18); //Configure the P0.18 pin as OUTPUT;

IOSET0 |= (1<<18);  //Make p0.18 pin as high

IOCLR0 |= (1<<18);   // Make p0.18 pin as low



Image 15

The C code starts with inclusion of 2148 header file

#include <lpc214x.h>


Next is the DELAY function which accepts an integer parameter count & creates delay


void delay_ms(unsigned int count)
unsigned int i=0,j=0;

Then starts the MAIN program

In LPC2148 each pin can perform 4 functions which is decided by setting PINSEL Register

Lpc2148 have two General Purpose I/O ports PORT0 & PORT1 ( both are 32-bit bi-directional ports).

In PORT0    P0.24, P0.26, P0.27 are not available for user & P0.31 is available for digital output only.

In PORT1 P1.0 to P1.15 is not available physically. Only P1.16 to P1.31 is available.

So out of 64 pins 45 pins are available for user

& these pins can perform multi functions decided by PINSEL Registers.

There are 3 such registers

PINSEL0 controls P0.0 to P0.15

PINSEL1 controls P0.16 to P0.31

PINSEL2 controls P1.16 to P1.31

PINSEL is 32 bit register , Each pin is allotted 2 bits so 16 x 2 = 32 , each PINSEL controls 16 pins

Check out the following table


For e.g take P0.1 , the position of the pin is bits 2 & 3 of PINSEL0

If these bits are  00 then P0.1 will perform as GPIO

If the bits are 01 , P0.1 will perform as RxD

If the bits are 10 , P0.1 will perform as PWM3

& if the bits are 11 , P0.1 will perform as EINT0

So if you want P0.1 as RxD , make the bits 2 & 3 as 01

0 0 0 0     0 0 0 0    0 0 0 0    0 0 0 0      0 0 0 0    0 0 0 0    0 0 0 0    0 1  0 0

PINSEL0 = 0X000000004;  is the code that makes it


By default , all pins perform as GPIO .Even if you do not define PINSEL , the pins will perform as GPIO.

But it’s a good coding practice to define PINSEL




In our code we use P0.18 to connect with an LED .P0.18 falls under PINSEL1

PINSEL1 = 0x000000;  //Configure the P1 Pins for GPIO;


Next we need to define the DIRECTION of the GPIO pin .2 Registers are available to control the Direction

IODIR0 – for PORT0 pins

IODIR1 – for PORT1 pins

Writing 1 makes the pin as OUTPUT

writing 0 makes the GPIO as INPUT

  To make P0.18 as OUTPUT write 1 at 18th bit

so IODIR0 = 0X00040000;  makes P0.18 as OUTPUT

Image 30

We can also use the BIIT OPERATOR ,

LEFT SHIFT binary 1   18 times makes P0.18 as OUTPUT

IODIR 0 |= (1<<18); //Configure the P0.18 pin as OUTPUT

Always use  |= operator , which makes other bits unaffected

You can follow any method bit operator or Hex notation.

Next is the never ending loop , inside which we make the P0.18 as HIGH & after a delay make it LOW

thus blinking the LED connected to it.


IOSET0 |= (1<<18);  //Make p0.18 pin as high

       IOCLR0 |= (1<<18);   // Make p0.18 pin as low

2 Registers are used for this  IOSET & IOCLR

Writing 1 to IOSET register makes the pin HIGH

Writing 1 to IOCLR register makes the pin LOW

Writing 0 to these registers have no effect.


Save the code by clicking the SAVE icon

Click build icon or F7 button to build the project.


Image 16


If the code is perfect you get 0 errors & 0 warnings

Image 17

Now the HEX file is created .

Note the path of the HEX file , it is under your project folder –> OBJECTS


Image 18

There are 2 applications to upload the HEX file on to the LPC2148


Open the FLASH MAGIC application

Image 19

Click on OPTIONS & put a tick mark against “ USE DTR & RTS


Image 20

Select the IC as LPC2148

Image 22

COM port is the one allotted under Device Manager.

Image 23

Baud Rate is 9600 & Interface is ISP

Click on BROWSE & navigate to the location of the HEX file (under OBJECTs Folder)


Select the HEX file & click START button to UPLOAD the HEX on to LPC2148.

Note that I’m using a development board with USB upload facility (CP2102 built in).Otherwise you need to press the ISP & RESET buttons together for uploading the HEX file.

Image 24


There is also DEBUG facility in KEIL.

Click on DEBUG icon to start debugging.


Under OPTIONS for Target , go to DEBUG tab & select





Under VIEW you can select the PORT0 to see the OUTPUT



cooltext753793315    cooltext753790696