Detecting Light and movement with Arduino

When it comes to detecting light and movement with microcontrollers, one has quite a bit of options, after explaining each one, I will compare them for you at the bottom of this pots

LDR Light Dependent Resistor

This is the simplest one, and it only costs a few cents

Phototransistor

A photo transistor is in my oppinion a great tool to use with microcontrollers, very fast response time (Slightly slower than a photodiode), and can be used with the digital pins, the reason is that it works exactly like a transistor, but instead of enabling the current by providing voltage to the base, the current is provided from light !

So, if you were to connect the Emitter to ground, and the Collector to the digital pin, they would get connected when light is detected, that is it

Photo interrupter

A photo interrupter detects the presence, absence, or movement of an object by sensing when a light beam between an emitter and a receiver is blocked.

This is basically something you would find in your inkjet printer ! or in a factory (Conveyor belt), or most commonly, on en elevator (Door opens when an object is in the way)

The transmitter and receiver work together either side by side or opposing each other, when they are side to side, a mirror reflects the light onto the receiver

If you are trying to detect a red light or a light of the wavelength close to that of the interrupter, odds are that you can use the receiver on its own without the LED (Transmitter), simply do not power it up !

photodiode

PIR (Movement) sensors

PIR Sensors such as the HC-SR501 can detect movement, but they requier more than 1 component, the sensor itself, the plastic cover for the sensor (Pir Lens Cover creates a FRESNEL LENS), and an IC that amplifies the signal and analyzes it !

Project cloud electric meter

The cloud electric meter is a project consisting of a 3 parts

1- A microcontroller and a current transformer (As of now, I am using the SCT-013)
2- A server to collect the data from the microcontrollers
3- A mobile phone app to display the data

The second and third (Server and application) are beyond the scope of what we are doing here, here, I will be examining all the options for a microcontroller and current transformer, With and without the cloud part.

1- The no cloud setup

The first example we will be using is the current transformer (SCT-013) with a nodeMCU ESP8266 that shows data about the current on a screen, this is the simplest setup, and is for people who are here to figure out how to connect their transformer, To do this, you will need a 16 x 2 screen, and an LCD I2C Interface Adapter.

1- Connecting the LCD to an ESP8266 (Tutorials for arduino and ESP32 are all over the internet)
2- Connecting the Current Transformer
3- The software

Connecting the LCD through I2C board

Similar tutorials are all over the internet, this one is here so that I can have a compatible setup between all the devices in the IoT project, the way those 2 boards (The LCD and I2C board) are very popular, and are connected in more or less the same way in all tutorials, but since you are here, might as well make it as simple and complete as possible.

LCD displays require a lot of connected digital pins to work, but this is easily solved with a very cheap LCD-I2C board that connects to my 1602 (16×2) LCD display (Or 2004 20 x 4 LCD modules) with its 16 pins on one end, and connects to your microcontroller (ESP8266, ESP32, Or Arduino, etc…) via 2 I2C pins (SDA/SCL)

The LCD I2C Interface Adapter board is based of a popular 8-bit, remote I/O expander for the I2C bus namely the pcf8574T / ABX919… A remote expander can function as an LCD driver, but it also works for many other purposes, but that is beyond the scope of the simple instructions presented here, here we are interested in driving an LCD screen

The I2C address of can be set by setting the available solder points for addresses in the range of 0x20 – 0x27.

One thing you may want to know is that the jumper at one side of the nameless I2C board is for the LED backlight ! it can be fed through a separate power supply (Remember to combine the ground pins).

In this example, I am using an ESP8266 on a NodeMCU board.

Power

The ESP8266 and the ESP32 are 5V tolerant on the data pins (Proof here), So, because the linear voltage regulator on the ESP8266 NodeMCU board does not have a heatsink, we will be connecting the screen directly to the 5V power source, why burdain the small regulator with the screen !

So, on my I2C board, I connect the GND and VCC to the external power supply (Don’t power the board via USB and connect to the VIN, this is not how it works, VIN is for voltage input, not for output)

So, on my board, the geekcreit doit esp12f devkit v3 (In arduino studio, it is compatible with NodeMCU 1.0 – ESP-12E Module)

The data pins are connected as follows
SDA – D2 – GPIO-04
SCL – D1 – SPIO-05

You are done with this, now, how to print to the screen !

the first step is to find the I2C address, my board provides addresses in the range of 0x20 – 0x27. without soldering anything, the address is probably 0x20, but if not, we can figure it out.

To figure out the address of your LCD display, use the following code

#include <Wire.h>
void setup() {
Wire.begin(4, 5); // SDA, SCL
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices = 0;
Serial.println("Scanning...");
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
}
if (nDevices == 0) Serial.println("No I2C devices found\n");
else Serial.println("done\n");
delay(5000);
}

Are the ESP32 and ESP8266 5V tolerant (Yes they officially are)

This is a very old question, ever since Espressif removed the 5V tolerant statement from their datasheet no one felt safe connecting 5V directly to the digital input pins, but the news is out now, according to the CEO of Espressif himself, their boards are indeed 5V tolerant ON THE DIGITAL INPUT PINS

What pins are 5V tolerant exactly?

The IO pins in input state (sink) are 5V tolerant, Yet the power supply to the chip must be 3.3V (Most boards come with a regulator for this so it should not be a problem). other models do not come with a regulator, and in such a case, you will need to add the regulator, but even then you do not need a level shifter for the digital inputs. for the ESP32, The ones without an onboard regulator usually go for as little as $2.5 (5 boards for $12) , while the ones that come with a voltage regulator and a serial to USB adapter will set you back around $4.6 (3 for $14)

When pins on the Espressif microcontroller are set as output, they will use 3.3V logic, whether or not the difference in voltage between high and low will register on the other microcontroller/device is an issue related to the other microcontroller, from my experience, Arduino Uno works just fine.

Also note that analogue pins are a different story, the ADC pins use the power provided to the chip as a reference voltage. so a voltage divider is still required.

so in short, if you connect the 5V supply to the VIN pin (going through the onboard regulator), and use 5V logic on the digital pins while they are in input mode (Sink) you should good, and this is not just me, this is an official statement.

You may be wondering why is it not the in datasheet then ? The answer is, it used to be in the datasheet, but the company faced problems with people powering the chip itself with 5V so they omitted it to avoid confusion,

This is excellent news for someone like me who has to go through the hassle of logic level converters whenever coupling Arduino with ESP chips.

Before the CEO of the company made those statements, many people did their own experiments and found those results, but there were still doubts as to whether the results were conclusive or whether there was more to the story, a convincing experiment by ba0sh1.com did demonstrate that it was indeed 5 Volt tolerant on the input pins,

Where did i get this from

Swee-Ann Teo, who after my research seems to be from Espressif made the following statements

  • On whether ESP8266 is 5V tolerant, he had this to say on a facebook post by hackaday

“i can reply officially here: it is 5V tolerant at the IO. while the supply voltage is at 3.3V.”

  • On whether ESP32 and ESP8285 are also 5V tolerant

“ESP32 and ESP8285 are both 5V tolerant as well. but for ESP32, it is a very complicated matter. it supports 1.8V operations too… i don’t know where to start…”

  • When asked why this information is not in the datasheet, he responded

“the reason is too many users took it to mean that the chip is 5 V tolerant. When we say 5 V tolerant, we are only referring to the IOs. So some users mistook this to make that they can power the chip entirely off the 5 V supply. The correct usage is to use 5 V open for these 5 V tolerant pins, and only via only drain configuration.” And then elaborated on the matter with “I understand, but the time needed to do the iterations when mistakes were made, was too long. when the product was launched 5 V WiFi modules (with DCDC) were the norm. Many users saw “5 V” written in the specs and thought it could be a 1-1 replacement for such modules.”

  • One user asked if the tolerance towards 1.8 volts of the ESP32 was relevant to enabling battery operation, the response was no, specifically, Teo responded with

“actually not. but many memory devices are moving towards 1.8V operations, and we would be compatible with them as well.”

The facebook post where this is all written is here.