Control Lights With Pressure Sensor & ESP8266

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

Once in a while, we come across an article about "Life Hacks That Will Change Your Life" or "Life Hacks Tips". Of course, they give us some very useful tips about life and small things by which we can make our life simpler and convenient. So, we decided to give a small but a very helpful technology hack for you.

We all enjoy reading our favorite books on our chairs. Everyone is so eager that we forget sometimes to switch ON the side lamps. Then, we have to get up from our chair and switch it ON. We know, it's very frustrating. What if, when we seat and side lamps turn ON automatically and it turns OFF again when we get up. It will be the best hack ever. Sounds very exciting and useful. Right !! Let's make it then.

Step 1: Equipment We Need

1. MPL115A2 Pressure Sensor

The MPL115A2 employs a MEMS pressure sensor with a conditioning IC to provide accurate pressure measurements from 50 to 115 kPa. It will act as your linking part between your movement and side lamp.

2. MCP23008 Relay Controller

MCP23008 from Microchip is an Integrated port expander that controls eight relays through the I²C bus. You can add more relays, digital I/O, analog to digital converters, sensors, and other devices using the integrated I²C expansion port.

3. Adafruit Huzzah ESP8266

The ESP8266 is an incredible platform for IoT application development. The ESP8266 processor from Espressif is an 80 MHz microcontroller with a full WiFi front-end and TCP/IP stack with DNS support as well. The ESP8266 provides a mature platform for monitoring and control applications using the Arduino Wire Language and the Arduino IDE.

4. ESP8266 USB Programmer

This ESP8266 host adapter was created specifically by Contol Everything for the Adafruit Huzzah version of the ESP8266, allowing I²C communication connections.

5. I²C Connecting Cable

Contol Everything also designed the I²C connection cable which is available at the above link.

6. Mini USB Cable
The mini USB cable power supply is a perfect choice for powering the Adafruit Huzzah ESP8266.

7. 12V Power Adapter

MCP23008 Relay controller ideally works on 12V and this can be simply given by a 12V Power Adapter.

Step 2: Hardware Connections

In general, making connections is the simplest part of this project. Follow the instructions and images, and you should have no problems.

1. Connection of Adafruit Huzzah ESP8266 and USB Programmer

First of all, take the Adafruit Huzzah ESP8266 and place it on the USB Programmer (with Inward Facing I²C Port). Press the ESP8266 gently into the USB Programmer and we are done with this step (See picture #1).

2. Connection of the Sensor and Adafruit Huzzah ESP8266

Take an I²C Cable and connect it to the Input port of the Sensor. For proper operation of this cable, please remember I²C Output ALWAYS connects to the I²C Input. Now, connect the other end of the same I²C Cable to the USB Programmer with Adafruit Huzzah ESP8266 mounted over it (See picture #2).

Note: The brown wire should always follow the Ground (GND) connection between the output of one device and the input of another device.

3. Connection of the Sensor and MCP23008 Relay Controller

Take another I²C Cable, connect one end to the Output port of the Sensor and another end on the Input port of the MCP23008 Relay Controller.

4. Powering of the Circuit

Plug in the Mini USB cable into the power jack of Adafruit Huzzah ESP8266. Also, don't forget to power up the Relay Controller with 12V Power Adapter. Plug it in and we are good to go. The final connections will look like picture #3.

5. Connecting Your Side Lamp to MCP23008 Relay Controller
Connect your side lamp that you wish to control on the relay part of the MCP23008 Relay Controller. Make sure you take all the precautions before doing this step.

Now, just place the sensor below the seat on your chair and we are done we the connections part.

Step 3: Code

The code for the MCP23008 Relay Controller and Pressure Sensor is available on our GitHub repository.

Before going on to the code, make sure you read the instructions given in the Readme file and setup your Adafruit Huzzah ESP8266 accordingly. It will take just 5 minutes to set up the ESP.

For your convenience, you can copy the working ESP code for this sensor from here also:

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works. // Control Lights with ESP8266 // This code is designed to work with the MPL115A2_I2CS I2C Mini Module available from // //

#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <Wire.h>

// MPL115A2 I2C address is 0x60(96) #define Addr_Sensor 0x60 // MCP23008_I2CR8G5LE I2C address is 0x20(32) #define Addr_Relay 0x20

const char* ssid = "your ssid network"; const char* password = "your password";

unsigned int data[8]; float a1 = 0.0, b1 = 0.0, b2 = 0.0, c12 = 0.0;

ESP8266WebServer server(80);

void handleroot() { // Start I2C Transmission Wire.beginTransmission(Addr_Relay); // Select IODIR register Wire.write(0x00); // All pins are configured as output Wire.write(0x00); // Stop I2C transmission Wire.endTransmission(); delay(500);

for (int i = 0; i < 8; i++) { // Start I2C Transmission Wire.beginTransmission(Addr_Sensor); // Select data register Wire.write(4 + i); // Stop I2C Transmission Wire.endTransmission();

// Request 1 byte of data Wire.requestFrom(Addr_Sensor, 1);

// Read 1 byte of data // A10 msb, A10 lsb, Bb1 msb, Bb1 lsb, B2 msb, B2 lsb, C12 msb, C12 lsb if (Wire.available() == 1) { data[i] =; } }

// Convert the data to floating points a1 = ((data[0] * 256.0) + data[1]) / 8.0; b1 = ((data[2] * 256) + data[3]); if (b1 > 32767) { b1 -= 65536; } b1 = b1 / 8192.0; b2 = ((data[4] * 256) + data[5]); if (b2 > 32767) { b2 -= 65536; } b2 = b2 / 16384.0; c12 = ((data[6] * 256.0 + data[7]) / 4.0) / 4194304.0; delay(300);

// Start I2C Transmission Wire.beginTransmission(Addr_Sensor); // Send Pressure measurement command Wire.write(0x12); // Start conversion Wire.write(0x00); // Stop I2C Transmission Wire.endTransmission(); delay(300);

// Start I2C Transmission Wire.beginTransmission(Addr_Sensor); // Select data register Wire.write(0x00); // Stop I2C Transmission Wire.endTransmission();

// Request 4 bytes of data Wire.requestFrom(Addr_Sensor, 4);

// Read 4 bytes of data // pres msb, pres lsb, temp msb, temp lsb if (Wire.available() == 4) { data[0] =; data[1] =; data[2] =; data[3] =; }

// Convert the data to 10-bits int pres = (data[0] * 256 + (data[1] & 0xC0)) / 64; int temp = (data[2] * 256 + (data[3] & 0xC0)) / 64;

// Calculate pressure compensation float presComp = a1 + (b1 + c12 * temp) * pres + b2 * temp;

// Convert the data float pressure = (65.0 / 1023.0) * presComp + 50.0; float cTemp = (temp - 498) / (-5.35) + 25.0; float fTemp = cTemp * 1.8 + 32.0;

// Output data to serial monitor Serial.print("Pressure : "); Serial.print(pressure); Serial.println(" kPa"); Serial.print("Temperature in Celsius : "); Serial.print(cTemp); Serial.println(" C"); Serial.print("Temperature in Fahrenheit : "); Serial.print(fTemp); Serial.println(" F"); delay(500);

// Output data to Web Server server.sendContent ("<html><head><meta http-equiv='refresh' content='2'</meta>" "<h1 style=text-align:center;font-size:300%;color:blue;font-family:britannic bold;>CONTROL EVERYTHING</h1>" "<h3 style=text-align:center;font-family:courier new;><a href= target=_blank></a></h3><hr>" "<h2 style=text-align:center;font-family:tahoma;><a href= \n" "target=_blank>MPL115A2 Sensor I2C Mini Module</a></h2>"); server.sendContent ("<h3 style=text-align:center;font-family:tahoma;>Pressure : " + String(pressure) + " kPa"); server.sendContent ("<h3 style=text-align:center;font-family:tahoma;>Temperature in Celsius = " + String(cTemp) + " C"); server.sendContent ("<h3 style=text-align:center;font-family:tahoma;>Temperature in Fahrenheit = " + String(fTemp) + " F");

if (pressure > 136) { // Start I2C Transmission Wire.beginTransmission(Addr_Relay); // Select GPIO register Wire.write(0x09); // Realy-1 set to logic High Wire.write(0x01); // Stop I2C transmission Wire.endTransmission();

// Output data to serial monitor Serial.println("Side Lamp : ON");

// Output data to Web Server server.sendContent ("<h3 style=text-align:center;font-family:tahoma;> Side lamp : ON"); } else { // Start I2C Transmission Wire.beginTransmission(Addr_Relay); // Select GPIO register Wire.write(0x09); // Relay-1 set to logic Low Wire.write(0x00); // Stop I2C transmission Wire.endTransmission();

// Output data to serial monitor Serial.println("Side Lamp : OFF");

// Output data to Web Server server.sendContent ("<h3 style=text-align:center;font-family:tahoma;> Side Lamp : OFF"); } }

void setup() { // Initialise I2C communication as MASTER Wire.begin(2, 14); // Initialise serial communication, set baud rate = 115200 Serial.begin(115200);

// Connect to WiFi network WiFi.begin(ssid, password);

// Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid);

// Get the IP address of ESP8266 Serial.print("IP address: "); Serial.println(WiFi.localIP());

// Start the server server.on("/", handleroot); server.begin(); Serial.println("HTTP server started"); }

void loop() { server.handleClient(); }

Step 4: Working

Download (gitpull) or copy the code and open it in the ArduinoIDE.

Compile and Upload the code and see the output on your Serial Monitor.

Note: Before uploading, make sure you enter your SSID network and password in the code.

Now, copy the IP address of ESP8266 from the Serial Monitor and paste it in your web browser. You will see a web page with pressure reading and status of your side lamp. Before moving to the final testing, you have to adjust the pressure value according to your requirements in the if-else condition in the code. When you will sit on your chair, the pressure will increase and it will automatically turn ON your side lamp. Similarly, when you will get-up, it will turn OFF the side lamp.

The output of the sensor on the Serial Monitor and Web Server are shown above.

Step 5: Applications and Upgrading

With this, you can control your side lamp automatically. No need of going near to the power buttons and turning them on/off every time when you need it.

But sometimes, you just need your lights off when you sit on the chair. For that, you can upgrade this project by adding an ON/OFF button on the web page. Now, you can control the lights automatically and manually also. You have to just pick up your phone, type in the IP address of your ESP and just turn on/off according to your requirement.

As the sensor can also read temperature and humidity, you can also control your AC/Heater according to your need. Just connect your AC/Heater with Relay Controller, modify the same code and you are done.

Life is more simple now, right !!

Step 6: Resources for Going Further

For more information about MPL115A2, MCP23008 Relay Controller and ESP8266, check out the links below:

Also, you can check our blog on Home Automation with Light Sensor and ESP8266.



    • Build a Tool Contest

      Build a Tool Contest
    • Faux-Real Contest

      Faux-Real Contest
    • Sweet Treats Challenge

      Sweet Treats Challenge