Categories

Arduino 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.

Here is a low cost module available that makes the sensor easier to work with

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 Arduino Pin
 Vin 5v
 Gnd Gnd
 SCL
SDA

 

 

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

Share

Arduino and keyes RGB Led module example

This example was for an RGB Led display I saw. It contains 8 led’s, 3 control lines for the red, green and blue and also requires a 5v connection. The LEDs are common cathode types so a low (0v) switches them on and a high (5v) switches them off.

So a lot of pins required for an Arduino Uno out of the box. Its quite a niche little product but if you wanted a bar of RGB led’s this could be an effective solution

Here is a picture of the module

Connection

Vcc – Arduino 5v

D0 – D7 -> Connect these to Arduino pins 2 to 9

R, G, B -> Connect these to Arduino pins 10 to 12

Code

Simple example that cycles through the red, green and blue colours. You could add more colours, have different effects and so on.

void setup()
{
  //all pins are outputs and we are using 2 to 13
  for (int x =2;x<13;x++)
  {
    pinMode(x,OUTPUT);
    digitalWrite(x,HIGH);
  }
}
 
void loop()
{
   blue();
   cycle(); 
   green();
   cycle();
   red();
   cycle();
}
 
void blue()
{
  digitalWrite(12,LOW);
}
 
void green()
{
  digitalWrite(11,LOW);
}
 
void red()
{
  digitalWrite(10,LOW);
}
 
 
void cycle()
{
  for(int x=2;x<10;x++)
  {
    digitalWrite(x,LOW);
    delay(500);
    digitalWrite(x,HIGH);
  }
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
  digitalWrite(12,HIGH);
}

 

Links

The module comes in at the $5 mark
Full Color LED Module / SCM light water

Share

Arduino FreeRTOS example

Out of the box the Arduino IDE does not support multi-tasking effectively. the good news its easy to add support via a library to the Arduino IDE

Most operating systems appear to allow multiple programs or threads to execute at the same time. This is called multi-tasking. In reality, each processor core can only be running a single program at any given point in time. A part of the operating system called the scheduler is responsible for deciding which program to run when, and provides the illusion of simultaneous execution by rapidly switching between each program.

The scheduler in a Real Time Operating System is designed to provide a predictable execution pattern. Traditional real time schedulers, such as the scheduler used in FreeRTOS, achieve determinism by allowing the user to assign a priority to each thread of execution. The scheduler then uses the priority to know which thread of execution to run next. In FreeRTOS, a thread of execution is called a Task.

 

Installation

The following is for later versions of the Arduino IDE that support importing

Navigate to Sketch -> Include Library -> manage Libraries
Type in RTOS and Install the FreeRTOS library

Here is a screenshot of the Library Manager – you can see I have installed the library

Hardware

I decided to use the Easy Module shield as it has many analog inputs and digital outputs, so for a basic example its easy to use some of these. The FreeRTOS library does come with a couple of built in examples, this example will be similar

I will blink the red LED and take readings from the LDR and the pot on the board. Here is a FreeRTOS example, now this is probably overkill for something as basic as this but it gives you an idea of a fairly simple example.

In the real world working with microcontrollers, RTOS are popular so it doesn’t do any harm learning in a nice easy environment like the Arduino

 

Code

#include <Arduino_FreeRTOS.h>
 
// define three tasks for Blink & LDR & VR
void TaskBlink( void *pvParameters );
void TaskLDR( void *pvParameters );
void TaskVR( void *pvParameters );
 
// the setup function runs once when you press reset or power the board
void setup() 
{
 
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
 
  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  (const portCHAR *)"Blink"   // A name just for humans
    ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  3
    ,  NULL );
 
 
  xTaskCreate(
    TaskVR
    ,  (const portCHAR *) "VR"
    ,  128  // Stack size
    ,  NULL
    ,  2  // Priority
    ,  NULL );
 
  xTaskCreate(
    TaskLDR
    ,  (const portCHAR *) "LDR"
    ,  128  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL );
 
}
 
void loop()
{
  // Empty. Things are done in Tasks.
}
 
/*---------------------- Tasks ---------------------*/
 
void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(12, OUTPUT);
 
  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(12, LOW);   // turn the LED on
    Serial.println("LED 12 on");
    vTaskDelay( 1000 / portTICK_PERIOD_MS );
    digitalWrite(12, HIGH);    // turn the LED off
    Serial.println("LED 12 off");
    vTaskDelay( 1000 / portTICK_PERIOD_MS );
  }
}
 
void TaskLDR(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  for (;;)
  {
    // read the input on analog pin 1:
    int sensorValue = analogRead(A1);
    // print out the value you read:
    Serial.print("LDR value = ");
    Serial.println(sensorValue);
    vTaskDelay(20);  // one tick delay (300ms) in between reads for stability
  }
}
 
void TaskVR(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  for (;;)
  {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.print("VR value = ");
    Serial.println(sensorValue);
    vTaskDelay(20);  // one tick delay (300ms) in between reads for stability
  }
}

 

If you open the Serial monitor you will be able to read the debug strings, adjust the pot, cover the LDR to change the values

Share

Run Arduino IDE as a portable version

One nice feature that has been introduced in the Arduino IDE is the ability to run the software as a portable version. The portable version will store the sketchbook, the libraries and the hardware folder locally where you want it.

This is handy for a number of scenarios but for me personally I like this as you could have a copy on a pen drive and move this from PC to PC and the big one is that having used the Arduino IDE for development on many platforms such as Arduino, Chipkit, ESP8266 and even ATTiny microcontrollers I have been looking for a way where you could just have a set of sketches and libraries just for that platform. Using this method you could have an installation for each one with just the supported core, libraries and sketches.

Its extremely easy to create the portable version

Download the compressed version of the Arduino IDE
Once download has been completed, extract the archive content to the location you want. e.g F:\ARDUINO
Open the extracted folder and create a new directory called portable in the root

The first time you open the IDE the structure will be populated and from that point on all the sketches, libraries and additional cores will be installed in the portable folder. You could copy the portable folder to another unzipped installation and all the settings from the first would be in place.

Share