Wemos and MAX44009 ambient light sensor example

The MAX44009 ambient light sensor features an I²C digital output that is ideal for a number of portable applications such as smartphones, notebooks, and industrial sensors. At less than 1µA operating current, it is the lowest power ambient light sensor in the industry and features an ultra-wide 22-bit dynamic range from 0.045 lux to 188,000 lux.

Low-light operation allows easy operation in dark-glass applications.

The on-chip photodiode’s spectral response is optimized to mimic the human eye’s perception of ambient light and incorporates IR and UV blocking capability. The adaptive gain block automatically selects the correct lux range to optimize the counts/lux.

 

Features

Wide 0.045 Lux to 188,000 Lux Range
VCC = 1.7V to 3.6V
ICC = 0.65µA Operating Current
-40°C to +85°C Temperature Range
Device Address Options – 1001 010x and 1001 011x

 

Connection

Module Pin Wemos Pin
 Vin 5v
 Gnd Gnd
 SCL  D1
SDA  D2

 

Code

#include<Wire.h>
 
#define Addr 0x4A
 
void setup()
{
 
Wire.begin();
// Initialise serial communication
Serial.begin(9600);
 
Wire.beginTransmission(Addr);
Wire.write(0x02);
Wire.write(0x40);
Wire.endTransmission();
delay(300);
}
 
void loop()
{
unsigned int data[2];
Wire.beginTransmission(Addr);
Wire.write(0x03);
Wire.endTransmission();
 
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
 
// Read 2 bytes of data luminance msb, luminance lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
 
// Convert the data to lux
int exponent = (data[0] & 0xF0) >> 4;
int mantissa = ((data[0] & 0x0F) << 4) | (data[1] & 0x0F);
float luminance = pow(2, exponent) * mantissa * 0.045;
 
Serial.print("Ambient Light luminance :");
Serial.print(luminance);
Serial.println(" lux");
delay(500);
}

 

Output

Open the serial monitor and change the light intensity on the sensor, here is an example

Ambient Light luminance :4.59 lux
Ambient Light luminance :6.12 lux
Ambient Light luminance :6.12 lux
Ambient Light luminance :36.72 lux
Ambient Light luminance :36.72 lux
Ambient Light luminance :36.72 lux
Ambient Light luminance :73.44 lux
Ambient Light luminance :73.44 lux
Ambient Light luminance :73.44 lux
Ambient Light luminance :9.95 lux
Ambient Light luminance :9.95 lux
Ambient Light luminance :12.24 lux
Link

https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf

MAX44009 Ambient Light Sensor Module with 4P Pin Header

Wemos and Soil Moisture Sensor example

In this article, we are going to interface a Soil moisture sensor with an ESP8266. This sensor measures the volumetric content of water inside the soil and gives us the moisture level as output. The sensor is equipped with both analog and digital output.

The soil moisture sensor consists of two probes which are used to measure the volumetric content of water. The two probes allow the current to pass through the soil and then it gets the resistance value to measure the moisture value.

When there is more water, the soil will conduct more electricity which means that there will be less resistance. Therefore, the moisture level will be higher. Dry soil conducts electricity poorly, so when there will be less water, then the soil will conduct less electricity which means that there will be more resistance. Therefore, the moisture level will be lower.

Input Voltage: 3.3 – 5V
Output Voltage: 0 – 4.2V
Input Current: 35mA
Output Signal: Both Analog and Digital
Pin Out
The  soil moisture sensor  has four pins:

VCC: Power
A0: Analog Output
D0: Digital Output
GND: Ground
The Module also contains a potentiometer which will set the threshold value. This threshold value will be compared by the LM393 comparator. The output LED will light up and down according to this threshold value.

To connect the sensor in the analog mode, we will need to use the analog output of the sensor. When taking the analog output from the soil moisture sensor, the sensor gives us a value from 0 to 1023. The moisture is measured in percentage, so we will map these values from 0 to 100 and then we will show these values on the serial monitor.

Connections

The connections for the soil moisture sensor to the Wemos are as follows:

VCC of the sensor to 3v3 of the Wemos
GND of the sensor to GND of the Wemos
A0 of the sensor to A0 of the Wemos

Analog Example

int sensor_pin = A0;
int value ;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Reading");
  delay(2000);
}
 
void loop()
{
 
  value= analogRead(sensor_pin);
  value = map(value,550,0,0,100);
  Serial.print("Moisture : ");
  Serial.print(value);
  Serial.println("%");
  delay(1000);
}

Another example
/*
# the approximate moisture levels for the sensor reading
# 0 to 300 dry soil
# 300 to 700 humid soil
# 700 to 950 in water
*/
 
# define ledPin D4
# define sensorPin A0 
 
int trigger = 300; // set the level
 
void setup()
{
	Serial.begin(9600);
	pinMode(ledPin, OUTPUT);
	digitalWrite(ledPin, LOW); // turn off LED
}
 
void loop()
{
Serial.print("Moisture Sensor Value:");
Serial.println(analogRead(sensorPin)); // read the value from the sensor
if (analogRead(sensorPin) >= trigger) 
{
	digitalWrite(ledPin, HIGH); // turn on the LED
}
else
{
	digitalWrite(ledPin, LOW); // turn off LED
}
delay(500);
}

 

Digital example

To connect the soil moisture sensor in the digital mode, we will connect the digital output of the sensor to the digital pin of the Arduino. The Sensor module contains a potentiometer, which is used to set the threshold value. The threshold value is then compared with the sensor output value using the LM393 comparator which is placed on the sensor module.

The LM393 comparator compares the sensor output value and the threshold value and then gives us the output through the digital pin. When the sensor value is greater than the threshold value, the digital pin will give us 5V and the LED on the sensor will light up. When the sensor value will be less than this threshold value, the digital pin will give us 0V and the light will go down.

Connections
The connections for the soil moisture sensor and the Wemosin digital mode are as follows.

VCC of sensor to 3v3 of Wemos
GND of sensor to GND of Wemos
D0 of sensor to pin D3 of Wemos
LED positive to pin D4 of Wemos
LED negative to GND of Wemos

Code

int led_pin =D4;
int sensor_pin =D3;
 
void setup()
{
  pinMode(led_pin, OUTPUT);
  pinMode(sensor_pin, INPUT);
}
 
void loop()
{
  if(digitalRead(sensor_pin) == HIGH)
  {
    digitalWrite(led_pin, HIGH);
  }
  else
  {
    digitalWrite(led_pin, LOW);
    delay(1000);
  }
}

 

 

Wemos and DHT22 readings on an OLED display simple project

In this particular example we are going to simply display temperature. pressure and altitude readings from a DHt22 sensor on an OLED display. These are all the shields used in making this a simple project to build.

Requirements

1 x Wemos Mini
1 x Wemos Dual Base
1 x OLED Shield
1 x DHT Pro Shield

You can see what I assembled in the picture below, I could have stacked in one column but I wanted to see the OLED and also leave the DHt22 uncovered.

 

Code

Various libraries required – you can install these via the library manager, here are links to them

https://github.com/adafruit/DHT-sensor-library
https://github.com/adafruit/Adafruit_Sensor
https://github.com/sparkfun/SparkFun_Micro_OLED_Arduino_Library

 

 

 #include "DHT.h"
#include <Wire.h>  // Include Wire if you're using I2C
#include <SFE_MicroOLED.h>  // Include the SFE_MicroOLED library
 
#define DHTPIN D4     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define PIN_RESET 255  //
#define DC_JUMPER 0  // I2C Addres: 0 - 0x3C, 1 - 0x3D
 
MicroOLED oled(PIN_RESET, DC_JUMPER); // Example I2C declaration
DHT dht(DHTPIN, DHTTYPE);
 
void setup() 
{
  Serial.begin(9600);
  dht.begin();
  oled.begin();
  oled.clear(ALL);  // Clear the display's memory (gets rid of artifacts)
  oled.display();  
}
 
void loop() 
{
  // Wait a few seconds between measurements.
  delay(2000);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  oled.clear(PAGE); 
  oled.setFontType(0); // set font type 0, please see declaration in SFE_MicroOLED.cpp
  oled.setCursor(1, 3);
  oled.print("Humidity: ");
  oled.setCursor(1, 12);
  oled.print(h);
  oled.print(" %\t");
  oled.setCursor(1, 21);
  oled.print("Temp :");
  oled.setCursor(1, 30);
  oled.print(t);
  oled.print(" *C ");
  oled.display();  
}

 

Output

Here you can see the output

 

Links

You can get all the boards and shields for about $10

Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos

Double Socket Dual Base Shield for WeMos D1 Mini NodeMCU ESP8266 Diy PCB D1 Expansion board

64X48 IIC I2C LCD OLED LED Dispaly Shield for Arduino Compatible WeMos D1 Mini

DHT Pro Shield for WeMos D1 mini DHT22 Single-bus digital temperature and humidity sensor module sensor

Wemos and DS18B20 readings on an OLED display simple project

In this particular example we are going to simply display temperature. pressure and altitude readings from a DS18B20 sensor on an OLED display. These are all the shields used in making this a simple project to build.

Requirements

1 x Wemos Mini
1 x Wemos Dual Base
1 x OLED Shield
1 x DS18B20 Shield (not an official Wemos Shield)

You can see what I assembled in the picture below, I could have stacked in one column but I wanted to see the OLED and also leave the BMP180 uncovered.

Code

2 libraries required – you can install these via the library manager, here are links to them
https://github.com/sparkfun/SparkFun_Micro_OLED_Arduino_Library

https://github.com/PaulStoffregen/OneWire

 

#include <OneWire.h>
#include <SFE_MicroOLED.h>  // Include the SFE_MicroOLED library
#define PIN_RESET 255  //
#define DC_JUMPER 0  // I2C Addres: 0 - 0x3C, 1 - 0x3D
 
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
OneWire  ds(D2);  // on pin D2 (a 4.7K resistor is necessary)
MicroOLED oled(PIN_RESET, DC_JUMPER); // Example I2C declaration
 
void setup(void) 
{
  Serial.begin(9600);
  oled.begin();
  oled.clear(ALL);  // Clear the display's memory (gets rid of artifacts)
  oled.display();  
}
 
void loop(void) 
{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
 
  if ( !ds.search(addr)) 
  {
    ds.reset_search();
    delay(250);
    return;
  }
 
  if (OneWire::crc8(addr, 7) != addr[7]) 
  {
      Serial.println("CRC is not valid!");
      return;
  }
 
  // the first ROM byte indicates which chip
  switch (addr[0]) 
  {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end  
  delay(1000);
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad
 
  for ( i = 0; i < 9; i++) 
  {           
    data[i] = ds.read();
  }
 
  // Convert the data to actual temperature
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) 
    {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } 
  else 
  {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
 
  }
  oled.clear(PAGE); 
  oled.setFontType(0); // set font type 0, please see declaration in SFE_MicroOLED.cpp
  oled.setCursor(1, 3);
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  oled.print("Cels = ");
  oled.setCursor(1, 12);
  oled.print(celsius);
  oled.print(" *C");
  oled.setCursor(1, 21);
  oled.print("Fahr =");
  oled.setCursor(1, 30);
  oled.print(fahrenheit);
  oled.print(" *F");
  oled.display();  
 
}

 

Output

Here you can see the output

 

Links

You can get all the boards and shields for about $10

Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos

Double Socket Dual Base Shield for WeMos D1 Mini NodeMCU ESP8266 Diy PCB D1 Expansion board

64X48 IIC I2C LCD OLED LED Dispaly Shield for Arduino Compatible WeMos D1 Mini

DS18B20 temperature sensor module measurement module FOR WeMos D1 mini WIFI extension board