Input Devices

Navigation
  1. Connecting the sensors
  2. Arduino IDE

Connecting the sensors

For my final project I am using a temperature sensor DHT11.
You can find the datasheet under this link: DHT11 Sensor.
Later on I also decided to add a photocell.

I connected them to my sensor and display pins:

sensor

Arduino IDE

For the sensor to work with the Arduino IDE I downloaded a library from git: Adafruit Sensor.
And added it with the arduino library manager (sketch > library > .zip library).

I also added additional libraries with the library manager.
Search for DHT11 and adafruit sensor and install the first ones.

The codes are commented to help you understand.
This is the code for the DHT11 sensor:

                    
    #include "DHT.h"

    #define DHTPIN PB1   

    #define DHTTYPE DHT11   

    DHT dht(DHTPIN, DHTTYPE);

    void setup() {
        Serial.begin(115200);
        Serial.println("DHT11 started!");

        dht.begin();
    }

    void loop() {
        delay(1000);

        // Reading temperature or humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
        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;
        }


        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.println(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" *C \n");
    }
                    
                
sensor

However for the final project I did not want the sensor to be the only one and wanted to add an air quality sensor.
Unfortunately we did not have this sensor in the FabLab.

Then I thought about adding a photocell to maybe dimm the light later on.

This is my final code for this assignment:

                    
    #include "DHT.h"
    #include <SoftwareSerial.h\>

    #define DHTPIN PB1   
    #define DHTTYPE DHT11  

    // PINS for the LEDs and the photocell
    #define LED_PIN_ONE 11
    #define LED_PIN_TWO PB2
    #define PHOTOCELL A0

    DHT dht(DHTPIN, DHTTYPE);
    int photocellReading;

    void setup() {
        Serial.begin(115200);
        Serial.println("DHT11 started!");
        
        pinMode(PHOTOCELL, INPUT); // SENSOR

        pinMode(LED_PIN_ONE,OUTPUT);
        pinMode(LED_PIN_TWO,OUTPUT);

        digitalWrite(LED_PIN_ONE, HIGH);
        digitalWrite(LED_PIN_TWO, HIGH);
        
            
        dht.begin();
    }

    void loop() {
        //Activate the resistor
        digitalWrite(PHOTOCELL, HIGH);

        //Read the data from the photocell
        photocellReading = analogRead(A0);

        //Map values from 0 to 255
        //0 is dark
        int brightness = map(photocellReading, 0, 1023, 255, 0);
        
        Serial.print("Analog Photocell: ");
        Serial.println(brightness);
        
        delay(2000);

        // Reading temperature or humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
        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;
    }


        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.println(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" *C \n");
    }

And here you can see that it works:
I tested it with a flash light.

sensor