4. Electronics and prototyping#

For this part of the class, we could choose between the electronics prototyping unit, the computer controlled cutting unit, or the 3D-printing for repair unit. I chose the Electronics one because next year I believe it will be the most relevant skill for the master’s degree I want to pursue next year.

1. Purpose of the unit#

The purpose of this unit is to learn how to use and configure a microcontroller board and the software used to program them. This will enable us to program basic electronic applications with input and output devices.

2. Equipment#

I used Arduino Uno microcontroller boards, a DHT20 humidity and temperature sensor, a neopixel led light, a breadboard and some electric cables. Arduino is the name of the company producing the Arduino Uno microcontroller boards, but also the name of the software program used to write code and obtain information from your devices. So when we say we used Arduino, the meaning depends on the context.

The first step I undertook was to read the DHT20 sensor’s data sheet in order to learn about it’s working principles and important characteristics. It can be found here In this data sheet we can find a lot of characteristics like precision, tolerance, hysterisis etc. but what is the most interesting for us is this diagram here:

diagram

It is relevant for us because it shows how to connect the wires from the sensor to the Arduino Uno. A mistake I made was to face the DHT20 in the wrong direction, so make sure to always face it towards the cables.

3. The first circuit#

In these pictures, you can see the layout of the sensor connected to the Arduino, which is connected to my pc with a USB-cable.

layout

The power supply comes from the red (5V) and black (ground) cable. The yellow cable (Serial Clock port) is used to synchronize the communication between the DHT20 and the microprocessor. The blue cable (Serial Data port) exchanges the information (temperature and humidity).

3.1 The code#

This was the code I used to get the results from my sensor. The void setup code is run once when launching the Arduino. The void loop part is looped forever untill the power is cut off.

 #include "Arduino_SensorKit.h"  // this command calls from a pre-existing library. We need
                                 // to install the library beforehand for it to work

 #define Environment Environment_I2C  

 void setup() {

 Wire.begin();              // initializes the I2C bus by setting the SDA and SCL pins to inputs
 Serial.begin(9600);        // establishes the connection with the port, with a speed of 9600 bits
 Environment.begin();
 }

 void loop() {
   Serial.print("Temperature = ");               // prints the text "Temperature = "
   Serial.print(Environment.readTemperature());  // prints the actual temperature number
   Serial.println("C");                          // prints the unit (degrees celcius)
   Serial.print("Humidity = ");                  // same principle for the relative humidity
   Serial.print(Environment.readHumidity());      
   Serial.println(" %");
   delay(2000);`            // this command imposes a delay of 2000ms (2s) before running the loop again
 }

3.2 Error message#

At first I had this error message: “avrdude: ser-open(): can’t open device “COMX”: The system cannot find the path specified.”

This happens when your Arduino doesn’t know which USB-cable port to use (even if you only have 1 USB-cable connected to your pc). It is easily resolved by clicking on tools > port > select your port (mine here is COM10)

error message

3.3 results#

The code prints the results in the serial monitor, which is opened by clicking tools > serial monitor, or simply by clicking this icon in the top right corner loupe

This opens a monitor where I could see the following results:

resultats

4. The second circuit#

To go further, I wanted to write a program that uses the data from the sensor to give an external signal. I decided I wanted to use a neopixel RGB led light and make it turn green when the humidity is stable, and red when it shows a variation. Circuit-wise, I just had to connect 2 more cables from the led light to the 5V and ground ports of the breadboard, then another cable from the led to the Arduino.

4.1 The code#

I started with the previous code as a basis, then started to modify it. I started with a 5% variation, but quickly realized that this was quite a low sensitivity, so I lowered it to 1%. I used an if/else function just like you would in python. A great way to test if the conditional loop works before trying to use an output device like the neopixel light, is to write a Serial.print command saying yes/no or green/red so you can check in the serial monitor if it works. This was the code:

#include "Arduino_SensorKit.h"

#define Environment Environment_I2C

void setup() {
  Wire.begin();
  Serial.begin(9600);
  Environment.begin();
}

float h1 = 25;      // just write any start value here

void loop() {
  float h2 = h1;
  h1 = float Environment.readHumidity();
  if (h2 < h1*1.01 and h2 > h1*0.99) {
    Serial.print("green");}
  else {
    Serial.print("red");}

  Serial.print("Temperature = ");
  Serial.print(Environment.readTemperature());                
  Serial.println(" C");
  Serial.print("Humidity = ");
  Serial.print(Environment.readHumidity()); 
  Serial.println(" %");
  delay(2000);
  }

This code gave the temperature and humidity in the serial monitor just like in the first code, but it also said “green” if the humidity was stable, and “red” when it wasn’t (this was tested easily by blowing on the sensor => I could see the humidity numbers changing and it said “red” instead of “green”)

4.2 On to the final code#

It was now time to incorporate the neopixel light into to program. The code is mostly the same, but I had to incorporate the neopixel library to use its functions. Instead of putting a Serial.print command in the if/else loop, I put a command that tells the light to turn green or red. I commented the parts of the code that changed compared to the previous version. I used codestral (same as mixtral but made specifically for writing code) to help me find the right punctuation.

```

include “Arduino_SensorKit.h”#

include “Adafruit_NeoPixel.h” // NEW: includes the neopixel library#

//uncomment line below if using DHT20

define Environment Environment_I2C#

define PIXEL_PIN 6 // NEW: defines the digital pin connected to the NeoPixel LED as nr 6#

define NUM_PIXELS 1 // NEW: number of NeoPixels in the strip#

Adafruit_NeoPixel pixels(NUM_PIXELS, PIXEL_PIN, NEO_GRB); // NEW

void setup() { Wire.begin(); Serial.begin(9600); Environment.begin(); pixels.begin(); // NEW }

float h1 = 25;

void loop() { float h2 = h1; h1 = Environment.readHumidity() ; if (h2 < h11.01 and h2 > h10.99) { pixels.setPixelColor(0, pixels.Color(0, 255, 0)); } // NEW else { pixels.setPixelColor(0, pixels.Color(255, 0, 0)); } pixels.show(); Serial.print(“Temperature = “); Serial.print(Environment.readTemperature()); Serial.println(“C”); Serial.print(“Humidity = “); Serial.print(h1); Serial.println(” %”); delay(1000); } ```

4.3 Final results#

I tested the circuit by looking at the serial monitor, and by blowing on the sensor. I could see that when the humidity is unstable due to the blowing, the light turned red, then turns back to green after a few seconds, when the humidity becomes stable again. The experiment is showcased in this video: (turn on the sound to hear the blow) video