4. Electronic prototyping#

Table of Contents

This week’s workshops were an introduction to electronic prototyping. We used and programmed a microcontroller (Type : Raspberry Pi Pico) to collect data from sensors and to control some components.

Arduino?#

First, a quick introduction : what exactly is a microcontroller and is it the same as an Arduino ?
Arduino is an OpenSource company that produces hardware (microcontrollers) and software to controle the hardware. The main goal at the founding of the company was to provide simple and low-cost tools for non-engineers. In this course, we will use the software only, however the board that we use is based on the OpenSource plans distributed by Arduino.
The board we will be using (YD-RP2040) has various features ; among them are analog and digital input/output pins, a central microprocessor, an USB-C input and an RGB LED that we’ll use later.
We will program the board using the Arduino software, which is based on C and C++ programming languages.
Here you can see the pinout of our microcontroller:

Connecting devices to a microcontroller : basics#

We’re going to start off with a simple example of what you can do with an Arduino : controlling a servomotor. A servomotor is a motor that rotates a certain angle between 0° and 180° depending on the input signal it receives. This is a so-called output device, because you give him information, as opposed to an input device, like the humidity sensor we will see later which gives us information to process.
We’re going to make a circuit which enables us to control a servo using a potentiometer (variable resistance).

Wiring#

Here’s an image of the wiring I made. A servo has 3 inputs : A red (5V = USB), a brown (Ground) and an orange (to any digital output pin, I chose number 9) wire. The potentiometer also has 3 connections; one for the ground, one for the 5V USB and one for an analog input pin, I chose to use A0.

Code#

The code is pretty simple. In Arduino, you need to first setup some definitions with the instruction void setup and after run the code with the instrucion void loop. In this case, we need to tell the computer that we plugged the potentiometer at the analog input A0 and that the servo is at the 9th pin. I think if I just show you the code it will be self-explaining.

#include <Servo.h>
int ppin = A0; //pin where the potentiometer is attached to
int val; //define a value for later

void setup{
  Servo.attach(9); //Servo at pin 9 
}
void loop{
  val = analogRead(ppin); //val is now the value of the potentiometer output (analog value btw 0 & 1024)
  val = map (val, 0, 1024 ,0 , 180); //converts the analog values in degrees
  Servo.write(val); //the servo now turns according to the potentiometer
  delay(15); //leaves the servo some time to reach the value
}

Connecting the PC to the Board#

A part with which I struggled much more than expected was connecting the board to the PC. Here’s what’s worked for me :

  • Go to the electronics website of the course in section languages/C Arduino and follow the Long story short tutorial
  • Connect the board. In the Arduino software, select Generic RP2040 as board and the port that pops up. Run the program
  • It won’t work, but you’ll see that Generic RP2040 will now be shown as connected to an USB port (COM x). Select Raspberry Pi Pico as a board and run again. It should now work.

    Note : here, COM5 is displayed as a port because I already did the process before.

Humidity sensor and RGB LED#

Wiring#

For the DHT20 humidity sensor, which was our first assignment, I downloaded a specific library and I chose a plotter program which was preinstalled in that library.

I then started to setup the board and the wires. I read the datasheet of the sensor and used the relevant information. The datasheet showed me that the sensor is most accurate for temperatures between 10°C and 55°C and for RH rates in between 5% and 85%.
It also explains the ports of the sensor. It has 4 ports, which are, from left to right :

  • The power supply (at 3.3V ideally)
  • The SDA port or serial data port; which has to be connected to any SDA input pin (see pinout of the RP-2040)
  • The ground port
  • The SCL (serial clock) port; which has to be connected to the SCL port next to the SDA you used.

    For the wiring, I chose to use the pins 20 and 21 for SDA and SCL outputs respectively.

Code#

Plotter#

Afterwards, I ran the plotter program on Arduino and I obtained a plot of the temperature and humidity given by the sensor. However, before running the program, I had to change the DHT.begin; line with

Wire.setSDA(x);
Wire.setSCL(y);

x and y being the pins where I put the SDA and SCL outputs from the sensor, so 20 and 21 respectively in my case. I obtained this plot; where value 2 is the temperature and value 1 the humidity.

Humidity sensor with LED output#

The next assignment was to create an adressable RGB LED, which I chose to combine with the humidity sensor to create a color code (red if high humidity, green if normal).
I downloaded the Adafruit NeoPixel library and i inspired my code from the simple example.
Here is the code:

#include "DHT20.h" //humidity sensor
#include <Adafruit_NeoPixel.h> //RGB

DHT20 DHT(&Wire);
#define PIN 23 //The RGB LED is on pin 23 on our board
#define NUMPIXELS 1 //There is only one LED (NUMPIXEL = 1)
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //Part of the simple program, didn't quite understand the meaning

void setup()
{
  Wire.setSDA(20); 
  Wire.setSCL(21);//SDA output on pin 20, SCL on 21
  Wire.begin();
  Serial.begin(115200);
  Serial.println("Humidity, Temperature");
  pixels.begin();
}


void loop()
{
  pixels.clear(); //Reset the LED
  if (millis() - DHT.lastRead() >= 1000) //sets a 1s space in time between every read
  {
    DHT.read();//read values of the sensor
    for(int i=0; i<NUMPIXELS; i++) {
    if (DHT.getHumidity()<50)//If humidity is low
    {
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));//Green

    pixels.show();//The LED shows green
    }
    else{//No elif command
      if(DHT.getHumidity()>70){
      pixels.setPixelColor(i, pixels.Color(150, 0, 0));//Red

      pixels.show();
    }
    else {
      pixels.setPixelColor(i, pixels.Color(150,75,0)); //Orange

    pixels.show();

    }
    }
    }

  }
}

Here’s the result (The humidity is high at the beginning because I put my finger around the sensor):

There are many other possibilities of course, I could have also put a servo as an output device instead of a led to show the humidity. A high angle would mean a high humidity and a low angle a low humidity. If you’re willing to try, feel free to play around!

Sidenote : I’ve only shown humidity because the temperature variations were less obvious to show (for humidity, you just have to put your finger around the device)

CO2 measurements#

Finally, together with Jean, I did something very similar to what I just did with the humidity sensor but using a CO2 sensor instead (watch Jean’s doc to see the result !).