Light

In this section, we will add a sensor to the Photon that measures light. We will add a Grove Light Sensor, that reflects the approximated trend of the intensity of light and not the exact lumen. Lumen is a measure of the total quantity of visible light emitted by a source.

The sensor's data cable needs to be connected to an analog pin. Using the analogRead function, a value will be retrieved between 0 and 4095. A bright LED light over the sensor, yields a value around 2500. In a dimly lit room, a value around 1800 could be retrieved.

Note that Grove sensors are typically used together with a Grove board and special Grove connectors. However; if you do not have a Grove board, as in my case with the Photon, you can still connect these sensors using female-male jumper wires as shown below:

The Grove Light Sensor is shown below:

The pins are clearly marked on the sensor. Connect GND to ground and VCC to power on the breadboard. The bottom pin, SIG, should go to a data pin (I used A0). The third pin is not connected (NC).

With everything connected properly, update the sketch:

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>


#define DHTPIN 1     // what pin we're connected to


#define DHTTYPE DHT22        // DHT 22 (AM2302)

// Variables for humidity and temperature
double h, t;

// Variable for light
int l;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    dht.begin();

    // Setup Particle variables
    Particle.variable("temperature", t);
    Particle.variable("humidity", h);
    Particle.variable("light", l);

    // Analog port for light intensity
    pinMode(A0, INPUT);
}

void loop() {
    // Wait a few seconds between measurements.
    delay(10000);

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a 
    // very slow sensor)
    h = dht.getHumidity();
    // Read temperature as Celsius
    t = dht.getTempCelcius();
    // Read light intensity
    l = analogRead(A0);


    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
        return;
    }

    // Publish to Particle
    Particle.publish("temperature", String(t), 60, PRIVATE);
    Particle.publish("humidity", String(h), 60, PRIVATE);
    Particle.publish("light", String(l), 60, PRIVATE);

}

A variable called l, of type int is declared and in the setup() block, the pin mode of pin A0 is set to INPUT because we will read out the pin. In the loop() block, all you need is a simple analogRead(A0) to read the value. For now, an extra event is published called light. Note that, near the top of the code, an extra Particle variable is set as well.

Later, we will publish one event with multiple values. But first, let's see how we can measure movement.

results matching ""

    No results matching ""