Week 11: Input devices

The assignment:

1. Group assignemt: LINK

2. Individual assignment: Measure something: add a sensor to a microcontroller board that you have designed and read it

In class we learned what a sensor is, different sensor types and how to choose them e.g. sensors that measure temperature, light, motion, distance, location, orientation, moisture and many other things. We also were presented to 3 rules for every project:

  • 1. Do not overkill – the simpler, the better
  • 2. Always look for existing solutions first
  • 3. Do not forget about software – use libraries, specially for digital sensors
  • What I did

    First I wanted to learn about the DHT11 (temperature and moisture sensor) and the data it could read. I looked through some webpages to do so. I started here

    Among other things I could read that the average humidity is 60%, meaning that 60% of the air around me is water vapor. I made some further research and found that the ideal humidity range should be between 40% and 60%. Sources to support this:

    IAQ

    Science direct

    Working Environment magazine

    This is useful information for my final project. I could also read about how to prepare the code for the sensor in Arduino IDE. I did the following:

  • Add the library for working with the DHT11 sensor to the Arduino IDE. Then I chose the DHT sensor from the the library located in >File>examples>DHT11>ReadTempAndHumidity.
  • Library

  • Now I had to check the pins on the DHT11, where I could see that pin 2 is the data pin
  • DHT11

    The data pin from the sensor is connected to my board with pin 0. I corrected this in the code.

    Now I was ready to upload the code to my ATtiny board to give me some data on the temperature and the humidity in the room. This is what I did to program the ATtiny with a programmer. I connected this programmer:

    Programmer

    with the AatTiny board:

    Connector

    See the design file (KiCad) for the board here

    I checked the settings for board, port and programmer and then I uploaded the code from the library using >sketch>upload using programmer. Problems: I got some error messages here, but in the end I made it work

    Mistakes made and lessons learned:

  • I had wired the programmer to the ATtiny wrong.
  • In the code I had written the wrong pinout. It should be 0 and not 2.
  • Nevertheless I could make a serial connection.

    Serial Connection

    The sensor started to read the temperature and the humidity in the room. To test it I breathed on the sensor and in the serial monitor I could read this:

    Combining input and output

    I made a test where I wanted a certain threshold in the reading from the sensor to move a sensor. I took the code from input week and the code from output week and adjusted it with this peace of code:

    Code

    The full code is here

    Explanation of the code: To begin with I include the library for the sensor DHT11 and the servo in the code by writing: #include DHT11.h #include Servo.h Servo myservo;

    In void setup (runs only once) the baut rate is set to 9600 bits per second. This allows serial communication between the computer, the port and my board. I also define the pin number on which the servo is connected. In this case it is pin number 10 myservo.attach(10);

    Under void loop (runs continously) I write how I want the servo to move based on humidity. In this case I want the servo to move when the humidity level is 60% or higher. if (humidity >= 60) { for (int pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree

    And when the humidity level drops under 58% I want the servo to stop moving

    if (humidity <= 58) { for (int pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees

    This it what came out of it: