4. Electronic Prototyping#

This week I learned how to program a microcontroller like Arduino and the basics of electronics system assembly.

Electronic assembly#

A microcontroller is an integrated circuit that brings together the essential elements of a computer:

  • processor (to execute the machine instructions of computer programs)
  • memories (to store information/data)
  • peripheral units (such as timers or even analog-to-digital converters that give a binary number from an electrical voltage)
  • input-output interfaces (to be able to interact with “the outside world”)

The microcontroller I use is the Arduino Uno.

Pin#

This microcontroller has several pins that have several types of functionality. Pin type:

  • input / output (which can be either digital or analog)
  • ground (or gnd is used to close the electric current)
  • 5V / 3.3V (used to send a continuous electric current)
  • and other…

Using resistance#

Adding a resistor to the system is very important if the system does not contain one. Without the resistor, there will be a short circuit that could damage the equipment. The heating of the electrical wires can cause an electrical fire and it can even electrocute a person in contact with the circuit. In this example the LED does not include a resistor, so one must be added.

Arduino progamming#

Arduino uses a variant of C++ to program the microcontroller. The differences are that they work with a “setup” and a “loop” instead of a “main”. The setup function is only run once during startup. Then the “loop” function will repeat itself in an infinite loop until the system is turned off. The language also has additional functions specific to the use of the Arduino board.

Being in computer science, I had no trouble understanding the form of the language for the Arduino card but I had to do a lot of research to understand how the new functions worked and the interaction with the installed components.

RGB LED (output exemple)#

For the RGB LED I had to look for what type of information to send (as well as the minimum and maximum value that can be used) and what function to use to send this information. I found out that it needs three analog outputs (one for each color) and that the values ​​that can be sent are numbers between 0 and 255.

// function used to send an analog value on a certain pin
analogWrite("pin number", "value");

Some devices only need an on/off state, so a digital value can be used instead.

// function used to send a digital value on a certain pin
digitalWrite("pin number", LOW - HIGH);

Potentiometer (input exemple)#

A potentiometer is a tool that provides a variable resistance. By rotating the shaft of the potentiometer, we can change the measured electrical potential. The values ​​that can be measured by an analog input are coded on 10 bits and can therefore vary between 0 and 1023 (0 for 0V and 1023 for 5V).

// function that returns the analog value received on a certain pin
analogRead("pin number")

Temperature & RH sensor (input exemple)#

The DHT20 is a tool for measuring room temperature and humidity. You can check the DHT20 Datasheet for more information on how it works.

Reading data on this device is more complex than on a potentiometer. We will therefore use the library provided for this purpose.

This library provides basic scripts that can be used to interact and read data with the DHT20.

Here is an example of reading data from the DHT20:

Original designs#

RGB LED wave#

In this system, an RGB LED will alternate colors from red to green, from green to blue and from blue to red in a loop.

int red = 10;
int green = 9;
int blue = 11;

int EclairageRed = 255;
int EclairageGreen = 0;
int EclairageBlue = 0;

int Step = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(red, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(red, EclairageRed);
  analogWrite(green, EclairageGreen);
  analogWrite(blue, EclairageBlue);
  delay(100) ;   // wait 0.1 second (100 milliseconds)

  if (Step == 1) {
    EclairageRed -= 10;
    EclairageGreen += 10;
  }
  else if (Step == 2) {
    EclairageGreen -= 10;
    EclairageBlue += 10;
  }
  else {
    EclairageBlue -= 10;
    EclairageRed += 10;
  }

  if (EclairageRed < 0) {
    Step = 2;
    EclairageRed = 0;
    EclairageGreen = 255;
  }
  else if (EclairageGreen < 0) {
    Step = 3;
    EclairageGreen = 0;
    EclairageBlue = 255;
  }
  else if (EclairageBlue < 0) {
    Step = 1;
    EclairageBlue = 0;
    EclairageRed = 255;
  }
  // Serial.println(EclairageRed);
  // Serial.println(EclairageGreen);
  // Serial.println(EclairageBlue);
}

Servo speed selector#

In this system, the speed of the servo varies according to the measured electrical potential which can be modified using a potentiometer.

#include <Servo.h>

Servo myservo;   // create Servo object to control a servo

int MesureTension;
int add;
int potpin = A0;   // analog pin used to connect the potentiometer
int Speed = 1500;   // variable to read the value from the analog pin

float maxVal = (3.3 * 1023/5) / 2;

void setup() {
  Serial.begin(9600);
  myservo.attach(9);   // attach the servo on pin 9 to the Servo object
}

void loop() {
  MesureTension = analogRead(A0);   // on mesure la tension sur le port A0
  // et on l’attribue à la variable MesureTension
  // Serial.println(MesureTension) ; // on envoie la valeur mesurée à l’ordinateur

  add = 1000 * (MesureTension - maxVal) / maxVal;

  Serial.println(add);

  myservo.writeMicroseconds(Speed - add);

  delay(100);   // waits for the servo to get there
}