4. Electronic prototyping (Arduino)#
In this module, we learned some of the basics of Arduino. I chose to follow this training because I had already heard that fun and interesting projects can be done with it (see here). In addition to its potential use for the group project, I was also interested in learning it to be able to create projects on my own at home.
4.1 Getting familiar#
Here is the kit that has been lent to us.
Before we could do anything with the kit, we had to install the Arduino IDE which allows us to write code and send/receive information through the microcontroller.
To get familiar with the microcontroller, we followed the examples included in the IDE. You can access them by navigating to “Files” -> “Examples”. In each example, you will find a link that provides explanations of the code and the corresponding circuit.
4.1.1 The button#
One of the examples I tried can be found under “1. Digital” -> “Button”.
To understand how the code works, I read the comments in the code, which are very clear.
Here is the corresponding circuit from the tutorial:
We notice a small issue here: the materials in our kit are not exactly the same as those in the tutorial. The breadboard is slightly different, and our button has only two wires for connection. Using the schematic view of the circuit provided in the tutorial link, I was able to adapt it.
By clicking on “Upload”, we send the instructions from the code to the microcontroller.
One may have a problem arising because the software don’t know yet in which USB port it has to send the instructions. This is solved by going to “Tools” -> “Port” and selecting “Arduino Uno”.
4.1.2 The speaker#
Another example I tried is “toneMelody”, which allows you to play a melody through the speaker included in our kit! I followed the same method described in the previous section to do it.
Running the code of this example will play a little melody through the speaker !
4.2 Little challenge#
The final challenge of the module was to use the humidity and temperature sensor (DHT20) included in the kit and connect it to an RGB led that was also provided. To achieve this, we first needed to familiarize ourselves with the characteristics of the sensor, which can be found in the DHT20 data sheet as well as the circuit diagram.
By analysing the following table of absolute maximum electrical ratings, we conclude that we have to plug the VDD pin to 5 Volt pin.
We can notice that the SDA and SCL pins can’t be found directly on the microcontroller. By using the following sketch found on google image, we aknowledge that SDA and SCL pins correspond to A4 and A5 pins respectively of our microcontroller.
Here is a bad photo of my wiring just to get a visual on what is ressemble to.
It is possible to find prewritten code by including the DHT20 library. To do this, go to “Croquis“–>”Inclure une bibliothèque“–>”Gérer les bibliothèques” and search for DHT20. Once the library is installed, you will be able to find example codes related to the DHT20 in the “Exemples” section, such as the following:
//
// 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 --
By running the code, we can then consult the data (temperature and humidity) taken by the sensor :
The circuit of the RGB led can be found by searching the reference in the back of the led, here VMA318, and find in the selling website the following user manuel.
By mixing the code of the DHT20 with the “Blink” example, I was able to send the data taken by the sensor and inserting it in the informations send to the RGB led (see the code at the end). You can see here that the led is going from blue to red as I am blowing out on the sensor :
Finally, I decided to add the speaker to the circuit. To do so, I send informations to the speaker based on the data of the sensor. Here is the full code :
// AUTHOR : Altay Oralarkaya
// LICENCE : CC0 1.0 Universal
// All the additionnal lines from me to the DHT20 code are marked by "AAA".
const int redledPin = 10; // AAA
const int blueledPin = 7; // AAA
const int greenledPin = 9; // AAA
#include "DHT20.h"
DHT20 DHT;
uint8_t count = 0;
void setup()
{
// initialize the LED pin as an output:
pinMode(redledPin, OUTPUT); // AAA
pinMode(blueledPin, OUTPUT); // AAA
pinMode(greenledPin, OUTPUT); // AAA
Serial.begin(9600);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire.begin();
DHT.begin();
delay(1000);
}
void loop()
{
if (millis() - DHT.lastRead() >= 1000)
{
// READ DATA
uint32_t start = micros();
int status = DHT.read();
uint32_t stop = micros();
if ((count % 10) == 0)
{
count = 0;
Serial.println();
Serial.println("Type\tHumidity (%)\tTemp (°C)\tTime (µs)\tStatus");
}
count++;
// ------------------------------
// This concerns the RGB led :
analogWrite(redledPin, (DHT.getHumidity()-43)/25*255); // AAA
analogWrite(blueledPin,255 - (DHT.getHumidity()-43)/25*255); // AAA
// ------------------------------
// This concerns the speaker :
int noteDuration = 100; // AAA
tone(8,(DHT.getHumidity()-43)/25*1000); // AAA
int pauseBetweenNotes = noteDuration * 1.30; // AAA
delay(pauseBetweenNotes); // AAA
noTone(8); // AAA
//------------------------------
Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Serial.print("\t\t");
Serial.print(stop - start);
Serial.print("\t\t");
switch (status)
{
case DHT20_OK:
Serial.print("OK");
break;
case DHT20_ERROR_CHECKSUM:
Serial.print("Checksum error");
break;
case DHT20_ERROR_CONNECT:
Serial.print("Connect error");
break;
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
case DHT20_ERROR_READ_TIMEOUT:
Serial.print("Read time out");
break;
case DHT20_ERROR_LASTREAD:
Serial.print("Error read too fast");
break;
default:
Serial.print("Unknown error");
break;
}
Serial.print("\n");
}
}
As you can see above, I programmed it so the pitch of the speaker is going higher as humidity increase. You can found my final result here!