4. Outil Fab Lab sélectionné#
This week we had two trainings of four hours to learn how to use electronics such as Arduino and its components.
Arduino IDE#
Arduino IDE is an open-source software for writting arduino code (C++) and sending it from your computer to your Aduino board (or other).
Manage libraries#
Most arduino components come with there own Libraries. To add a library got to
Tools \(\rightarrow\) Manage Libraries
Adding a library will also give acces to new “example codes” related to it. Example code are in
File \(\rightarrow\) Examples
Serial Monitor#
The serial monitor is the interface on which your can print stuff such as written text or data retreived from your electronic components. The serial monitor runs continuously once you run the code. It is also where input must be given if asked in the code.
Once you run your code you can acces the serial monitor through the magnifying glass button in the top right corner.
This is if you enabled the serial monitor in your code. To do so, add the following in your code:
Serial.begin(9600);
The number in brackets designates the data rate communication in bits per second.
WARNING ! It is crucial for this number to be same in the baud option of your serial monitor.
Define variables#
It is often better and sometimes even necessary to define variables at the begining of the code.
For variables that define a fixed value, use
#define 'variable' 'value'
//ex
#define BUTTON_PIN 4
#define DELAYVAL 500
For a int/float variable needed for mathematical operation, use
Int x=0;
Arduino board#
I had some problems connecting the arduino board to my computer and sending my code to it.
I am a beginner user of archlinux and the way it is currently configurated on my computer makes it so that I need to add the ArduinoBoard to my rules.d folder so I can explicitly grant it permission to communicate with my computer. There is definitely a better work around, but this is the solution I learned to use.
First I need to obtain the ArduinoBoard’s info. With it connected to a USB port I run lsusb. Amongst others it should appear in the following way:
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 027: ID AAAA:BBBB Arduino Board // <---
Bus 001 Device 006: ID 100b:0aaa Intel Corp.
Bus 001 Device 022: ID 413c:2113 Dell Computer Corp.
I then go to the udev rules folder and create a new .rules file:
cd /etc/udev/rules.d
vim 00-ttyusb.rules
It is in this file that I put the arduino board’s ID
SUBSYSTEM=="tty", GROUP="plugdev". MODE="0660"
SUBSYSTEMS=="usb", ATTRS{idProduct}=="BBBB", ATTRS{idVendor}=="AAAA", SYMLINK+="arduino"
Finally I run
sudo udevadm control --reload-rules && sudo udevadm trigger
I can now change the permissions granted
chmod 666 /dev/ttyABC
ABC represents the USB port to which the arduino board is connected. I find it with
ls -l /dev/tty*
Using arduino components#
During the second training we learned how to get data from an external sensor and reuse it afterwards. The sensor was a DHT20 humidity and temperature (H&T) sensor. The goal was to show the data obtained with the DHT20 using an RGB LED.
DHT20#
- VDD - 5V (NOT 3.3V)
- SDA - Analogue 4 (A04)
- GND - ground
- SCL - Analogue 5 (A05)
Library: DHT20
Using the basic DHT20 example, once you run the code, humidity and temperature will be printed in the serial Monitor. Most of the code is for an elegant presentation of data. What enables to get the humidity and temperature data from our DHT20 is
DHT.getTemperature()
DHT.getHumidity()
This following part of the code is why the serial monitor prints out 10 H&T values before retyping the header.
if ((count % 10) == 0){
count = 0;
Serial.println();
Serial.println("Type\tHumidity (%)\tTemp (°C)\tTime (µs)\tStatus");
}
count++;
RGB LED#
Library: Adafruit NeoPixel
The use of a RGB LED is quite straightforward. You define the color of a pixel using three numbers x, y and z from 0-255 which define the amount of Red, Blue and Green.
pixels.setPixelColor(i, pixels.Color(x, y, z));
Note : i is the pixel you are defining, counting starts at 0
In the Simple example, the number of pixels is set to 16 and a forloop is used to define the value of each 16 pixels. In our cas we only need to use 1 pixel and therefor no forloop is needed.
DHT20 & RGD LED together#
I made the RGB LED’s pixel colour depend on the humidity data received from the DHT20 sensor. The humidity value is a float number from 0 to 100 (%). I turned this number into an integer and mapped it to a scale from 0 to 255 in order to light the pixel.
int H = int(DHT.getHumidity()) // Humidity value caught but DHT20
int x = map(H, 0, 100, 0, 255) // Mapping
pixels.setPixelColor(0, pixels.Color(x, 0, 0)); // Setting LED color
This way, only the intensity of the pixel depends on the humidity, not the color itself. I wanted to have a gradiant from blue to red.
pixels.setPixelColor(i, pixels.Color(x*(H*0.01), 0, x*(1-H*0.01)));
This way, higher humidity will mean more red and lower will mean more blue.
There is still a probleme though. I hadn’t noticed as the humidity couldn’t go lower then 40% in our testing conditions, but clearly here if the humidity is at 0, both red and blue values will be zero, resulting in no light at all.
The following changes would probabaly solve the issue:
int H = int(DHT.getHumidity())
int x = map(H, 0, 100, 0, 255)
int y = map(100-H, 0, 100, 0, 255)
pixels.setPixelColor(0, pixels.Color(x, 0, y));