Module 4 - Electronic Prototyping#

This week we learned how to use a microcontroller development board to prototype electronic applications by programming it and using input and output devices. We had 2 assignments given , one called DHT20 - Temperature & RH sensor :

  1. Read the sensor’s datasheet and identify relevant characteristics
  2. Connect the sensor to your development board
  3. Write a program to read data and send it to your computer over usb
  4. Write a program to process data and display some information using the onboard LED

and another one was called a Challenge : Make your board do something ,add an output device to a development board and program it to do something.

Be careful#

Knowledge of some basic principles in physics is necessary, in order not to damage the microcontroller and other objects used. That’s why we had a reminder on amperage, voltage, resistance . Here I will write some important things that I kept in mind:

  1. The total input/output current of the device should not be higher than 50mA.
  2. We need to chose the right resistance , to avoid burning the material because of the high amperage.
  3. In every circuit,the GND should be connected.

Here is a link that helped me to determine the resistor value, needed when made the circuits.

4.1 Arduino#

In order to get started I installed Arduino IDE with the command brew install --cask arduino-ide, but here you have the link where you can install it for the platform of your choice. Programming in Arduino was not something difficult for me, sure it was a new environment , but I managed to adapt fast to it. Here you have a useful guide on how to program your RP2040 microcontroller. Also the Arduino documentation and Built-in Examples, last one was very useful for me, and helped me understanding better how Arduino IDE works , and also gave me the opportunity to try new things, besides the assignments. You can find also this examples in the Arduino IDE by navigating to File->Examples, I used this at the beginning to get more familiar with the code and to start to communicate with the microcontroller . In each example from the IDE you can find a link that provides explanation of the code and details about how to implement the circuit.

4.2 How it started#

Like I said earlier , in order to be able to do the assignments, at the beginning I started with some built-in-examples for which I made the necessary circuits.

First one was Blink where a LED is turned on and off every second.

Second one was How to Wire and Program a Button where the button turns on the LED when is pressed.

I added also a light sensor that controls the brightens of the LED in depending of how much light the sensor has.

Besides this examples, I made also other things to practice and discover the Arduino Kit, like adding a potentiometer that controls the brightness of the LED, but I don’t think it’s relevant, the same principle is used for the circuit.

4.3 DHT20 - Temperature & RH sensor#

In order to work with the sensor DHT20 , it’s required to read the datasheet and also as part of assignment is to identify relevant characteristics. Below you can find images with the main information that I found useful for our assignment.

4.3.1 DHT20 Library#

The library corresponding to the DHT20 sensor needs to be installed. To do so I access Sketch -> libraries and add DHT20 lib. Now we also have access to code examples related to DHT20.

4.3.2 Testing DHT20#

In order to test the temperature and humidity sensor I used the already existent code . You can find it by going throw File->Examples->DHT20->DHT20_plotter. The code is as follows:

//
//    FILE: DHT20_plotter.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//     URL: https://github.com/RobTillaart/DHT20
//
//  Always check datasheet - front view
//
//          +--------------+
//  VDD ----| 1            |
//  SDA ----| 2    DHT20   |
//  GND ----| 3            |
//  SCL ----| 4            |
//          +--------------+


#include "DHT20.h"

DHT20 DHT(&Wire);


void setup()
{
  Wire.begin();
  DHT.begin();  //  ESP32 default pins 21 22

  Serial.begin(115200);
  Serial.println("Humidity, Temperature");
}


void loop()
{
  if (millis() - DHT.lastRead() >= 1000)
  {
    //  note no error checking
    DHT.read();
    Serial.print(DHT.getHumidity(), 1);
    Serial.print(", ");
    Serial.println(DHT.getTemperature(), 1);
  }
}


//  -- END OF FILE --

Here you can see the circuits made by me for this example and bellow is the result that is obtained after the code is running for some time:

As I blow with heat in the sensor, we can observe changes in the humidity and temperature.

4.3.3 DHT20 with onboard LED#

In this case I will add to my board a LED that is turning red when the temperature is more then 20 degree, and blue if less or equal. To work with this type of LED I installed the library Adafruit_NeoPixel. You can do it by the same way as DHT20. Bellow you can find the code that I created and also a image with the circuit. Like we can see, the LED is red color because the temperature is more then 20 degree, this is demonstrated by the graphic above.

//File : DHT20_led.ino

// Author : Melnic Alexandru

// License : Creative Commons Attribution-ShareAlike 4.0 International [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)

#include "DHT20.h"
#include <Adafruit_NeoPixel.h>
#define PIN        6 

Adafruit_NeoPixel pixels(1, PIN, NEO_GRB + NEO_KHZ800);

DHT20 DHT(&Wire);


void setup()
{


  Wire.begin();
  DHT.begin();  

  Serial.begin(9600);
  Serial.println("Humidity, Temperature");
  pixels.begin();
}


void loop()
{
  if (millis() - DHT.lastRead() >= 1000)
  {
    pixels.clear();

    //  note no error checking
    DHT.read();
    Serial.print(DHT.getHumidity(), 1);
    Serial.print(", ");
    Serial.println(DHT.getTemperature(), 1);

    if (DHT.getTemperature() > 20.0)
    {
      pixels.setPixelColor(0, pixels.Color(255, 0, 0));
      }
      else {
        pixels.setPixelColor(0, pixels.Color(0, 0, 255));
      }

    pixels.show(); 
  }
}
//  -- END OF FILE --

4.4 Challenge#

For the challenge I added to my circuit a servo motor. The idea is that the LED will be green and the servo motor will turn into a direction when the humidity is smaller then 65 , and for the other values the LED will turn red and the motor will change direction and increase the speed. This is the code that I wrote:

//File : DHT20_led_servo.ino

// Author : Melnic Alexandru

// License : Creative Commons Attribution-ShareAlike 4.0 International [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)

#include <Servo.h>
#include "DHT20.h"
#include <Adafruit_NeoPixel.h>
#define PIN        6 

Adafruit_NeoPixel pixels(1, PIN, NEO_GRB + NEO_KHZ800);
DHT20 DHT(&Wire);
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


void setup() {
  Wire.begin();
  DHT.begin();  

  Serial.begin(9600);
  Serial.println("Humidity, Temperature");
  myservo.attach(9); 
  // attaches the servo on pin 9 to the servo object

  pixels.begin();
}

void loop() {
  if (millis() - DHT.lastRead() >= 1000)
  {
    pixels.clear();
    //  note no error checking
    DHT.read();
    Serial.print(DHT.getHumidity(), 1);
    Serial.print(", ");
    Serial.println(DHT.getTemperature(), 1);

    if (DHT.getHumidity() < 65.0)
    {
      myservo.writeMicroseconds(2000);
      pixels.setPixelColor(0, pixels.Color(0, 255, 0));
      }
      else {
        myservo.writeMicroseconds(500);
        pixels.setPixelColor(0, pixels.Color(255, 0, 0));
      }
      pixels.show();


  }
}

As you can see in this video, the result is as expected, at first the humidity is less than 65, but after I blow close, it increases, the motor changes direction and speed, the LED turns red, after a short time the humidity returns below 65, and the components change their behavior accordingly.