4. Electronic Prototyping#

In this unit we nne to learn to use a microcontroller development board to prototype electronic applications by programming it and using input and output devices.

For the DHT20 - Temperature & RH sensor :

  • Read the sensor’s datasheet and identify relevant characteristics
  • Connect the sensor to our development board
  • Write a program to read data and send it to our computer over usb
  • Write a program to process data and display some information using the onboard LED

Challenge :

  • Make our board do something : add an output device to a development board and program it to do something

4.1. Getting started#

First thing first, we need to install the arduino IDE and make sure to take a version 1.8.X, since the newest version’s interface is a bit weird. To get used with the basics of arduino, there is a getting started tutorial on their website.

4.2. First day of formation#

We had a little course on electricity. It is important to be familiar with the basics, a reminder about amparage, voltage, resistance, etc … The main point of this is to make sure our material stays safe, it is really easy to damage the material if we don’t pay attention to how we use it with electricity. The main ennemy is the amps, if the amps are too high, we might burn the material and therefore break the microcontrols.

We were presented to an arduino Uno and we learned about the different types of ports. There are two main types of ports : * Analogic : can read all the values between the minimum value of voltage and the maximum value of voltage. The micorcontrolor can only take a mesure at a time, it doesn’t read continuosly the values. * Digital : only give a volue of 0 or 1.

We also learned how to use the different components such as resistors and LEDs. We need to be carefull with the voltage, the arduino uses 5V for input and output. Make sure to always use the GND in every systems and be carefull to use max 40mA per output and 200mA in total to avoid burning the board. Each group of pins can’t go above 100mA, the groups are differenciated by the color red and yellow. The ports TX (1) and RX (0) are used to communicate via the USB port.

4.2.1. First time using the arduino#

For the first time trying the arduino, we needed to simply make a blinking LED. By following the instructions of the blink example on the arduino website, we can easily achieve that. A convention to follow while using the cables is to always put red on the 5V and black on the GND pins.

When testing for the first time this contraption, I encountered the error avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied". To solve the problem I followed the steps explained in this askUbuntu question.

  • A link that is usefull is this one. There is a lot of informations regarding the arduino, the kit’s components, precautions, and much more.
  • For more details about the pin layout, you can check this link.
  • To learn more about the arduino Uno, follow this link.
  • If you don’t have an arduino, tinkercad allows you to simulate an arduino and learn how to use it.
  • To buy electronic material you can check the TinyTronics website.
  • Resistor color code

4.3. Second day of formation, DHT20 - Temperature & RH sensor#

On the second day, we had an assignement and a challenge. We first had to read the sensor’s datasheet then connect it to the board, write a program that would read the data and send it to the computer vias the USB port, and finally write a program to process the data to display it on the board using a LED.

After this assignement, the challenge is to use an output device to make something (using a motor, audio, printing, …)

We first went through the data sheet and here are some notes I quickly took while we were reading it :

4.3.1. Adding the lib in arduino IDE#

To be able to use the DHT20 sensor, we need to install a library. To do so we go to Sketch -> libraries and add the DHT20 lib. Once this is done, we can get code examples for the sensor. To do so, we go to File -> Examples -> DHT20, and you will get exemples for using the sensor. For the challenge I went with the DHT20_plotter example. Another lib I used was Neopixel to use different colors on the LED, all the information about it can be found here.

4.3.2. Connecting the sensor to the arduino and testing#

To get used to the sensor, we first tried to simply connect to the board and the arduino. Here is a picture of how I did it :

Once the sensor is correctly connected, we can go in the arduino IDE and take the DHT20_plotter example to test if the sensor works correctly. Here is the code from the example that has been modified to showcase different colors with the lib NeoPixel :

//
//    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 <Adafruit_NeoPixel.h>

#define PIN 12
#define NUMPIXELS 0

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

#include "DHT20.h"

DHT20 DHT(&Wire);


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

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


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);
  }

  pixels.clear();
  pixels.setPixelColor(0, pixels.Color(0, 150, 0));
  pixels.show();
}


//  -- END OF FILE --

Once we run the program, we can check the plot tool to see the following graph :

The blue line is the relative humidity and the red one is the temperature. When i put a finger on the sensor, we can see that the relative humidity rise up.

4.3.3. The challenge#

For the challenge I wanted to add to my contraption a Servo sweep motor. The plan was to change the LED color and activate the “fan” when the humidity was above a certain threshold. To do so I used the following examples available in the arduino IDE and combined them : DTH20_plotter, LED Neopicel simple and Motor Servo Sweep.

const int sensorPin = A0;
int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;

#include <Servo.h>

Servo myservo;

#include <Adafruit_NeoPixel.h>

#define PIN 12
#define NUMPIXELS 1

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

#include "DHT20.h"

DHT20 DHT(&Wire);

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

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

  myservo.attach(3);
}

void loop()
{
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, sensorMin, sensorMax);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

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

  if (DHT.getHumidity() < 50) {
      myservo.writeMicroseconds(1500);
    } else {
      myservo.writeMicroseconds(2000);
    }

  pixels.clear();

  pixels.setPixelColor(0, pixels.Color(sensorValue,sensorValue,sensorValue));

  pixels.show();
}

After connecting every component to the board and the arduino and running the code, here was the result :

As we can see, when I bring my finger near the sensor, the relative humidity rises. This leads to the LED turning red and the little fan starting to work.