Lab 9 - Arduino Libraries and Sensor Integration
In questa pagina 4
Continuation of Lab 8 - Analog Output and Sensors with ArduinoLaboratory 8 covers digital input modes, PWM analog output with analogWrite (LED fading), the 10-bit analogRead input, the map and constrain functions, trimmer and photoresistor exercises, the LM35 temperature sensor and two additional exercises (hysteresis and potentiometer-controlled data logger).Lab 8 - Analog Output and Sensors with Arduino → on the Arduino PlatformArduino is a family of open single-board microcontroller boards built on Atmel AVR chips (UNO, Mega, Micro), extended with stackable shields and programmed through its own IDE.Arduino Platform →.
Libraries
- A library is a set of functions that, in the case of a sensor, simplifies the interpretation of the transmitted data by taking care of the communication with the hardware.
- For the most popular components there are libraries developed and updated by the Arduino community. Sometimes several libraries exist for the same component: compare their characteristics, run tests and verify the best choice (also considering how recent they are).
- A library is typically a group of files in a compressed folder (
.zip) imported into the IDE from the main menu.
Two ways to install a library
- Add library from .zip file: select the
.zipalready downloaded from the developer (usually from GitHub) from its local path on the PC (IDE menu Sketch -> Include library -> Add .ZIP library). - Library manager (same menu, "Manage libraries"): a search tool that checks whether the library is among those recognized by the IDE and installs it.
Third, manual way (own or third-party libraries)
- An Arduino library must contain at least two files: a
.cor.cppfile (the program) and a.hfile (often also areadme.txtand anexamplesfolder with guide sketches). - The files are first organized in an unzipped folder, then compressed into a single
.zipfile with the same name as the.hand.cppfiles it contains. - The
.zipis then placed in the.../librariesfolder (created when the IDE is installed; the path depends on the operating system).
DHT11 temperature and humidity sensor
Properties from the slide:
| Quantity | Range | Accuracy |
|---|---|---|
| Temperature | 0 degC <= T <= 50 degC | ±2 degC |
| Relative humidity | 20 % <= RH <= 90 % | ±5 % |
- Cheap and reliable; for more accurate measurements the DHT22 is available.
Connection with Arduino (bare 4-pin sensor):
| Sensor pin | Connect to |
|---|---|
| Pin 1 | 5 V |
| Pin 2 | any digital I/O (e.g. pin 4) |
| Pin 3 | no connection |
| Pin 4 | GND |
| 10 kOhm resistor | in parallel (between 5 V and pin 4 as written on the slide) |
On the market the DHT11 is also sold already mounted on a small base, to which only power and GND (and the data line) have to be connected: always check the datasheet. (In the photograph the module has three pins labelled +, out and -.)
Library to insert: dht-sensor-library-master.zip (Adafruit DHT sensor library, GitHub repository adafruit/DHT-sensor-library).
NB: when using Adafruit libraries the Adafruit Sensor Library (GitHub adafruit/Adafruit_Sensor) must also be added; guide at learn.adafruit.com/dht/using-a-dhtxx-sensor.
LCD display (16 pins, 2 rows)
The slide shows a 16-column, 2-row character LCD and its pin table:
| Pin | Symbol | Function |
|---|---|---|
| 1 | VSS | GND |
| 2 | VDD | +5 V |
| 3 | V0 | contrast adjustment |
| 4 | RS | H/L register select signal |
| 5 | R/W | H/L read/write signal |
| 6 | E | H/L enable signal |
| 7-14 | DB0-DB7 | H/L data bus lines |
| 15 | A | +4.2 V for LED (backlight anode) |
| 16 | K | power supply for backlight (0 V) (cathode) |
Pin functions in more detail:
- Pin 1 (Vss): GND. Pin 2 (VDD): +5 V.
- Pin 3 (V0): character contrast control. Generally connected to a potentiometer or trimmer in a voltage-divider configuration, so that a voltage varying from 0 to +5 V can be applied to pin 3 and the contrast varies with it.
- Pin 4 (RS): register selection signal: selects the register in which what appears on the display is recorded, or the operating register in which the instruction to execute is stored each time.
- Pin 5 (R/W): selects read or write mode; connected to GND here (write only).
- Pin 6 (E, Enable): enables writing to the registers.
- Pins 7-14: data lines. A HIGH value indicates the WRITE of the bit in the display register; a LOW indicates a READ value from a register (as written on the slide).
- Pin 15 (A, anode): to a positive voltage (in this lab +4.2 V) for the backlight.
- Pin 16 (K, cathode): to GND for the backlight.
Anode and cathode (not connected in the figure) belong to the backlight LED and must be connected through a limiting resistor.
LCD with trimmer: wiring figure
The figure shows the display connected to the Arduino with the 4-bit data lines (only D4-D7 of the display are wired, as seen from the labels D4, D6, D7 on the figure) plus RS and E on digital pins, Vss to GND, Vdd to 5 V, R/W to GND, and a potentiometer between 5 V and GND whose wiper goes to V0 (contrast). The exact pin numbers are not legible in the figure and are therefore not transcribed; consult the LiquidCrystal example on arduino.cc.
LiquidCrystal library
- Drives displays compatible with the Hitachi HD44780 driver.
- Can be installed from the IDE's library manager (check whether already present) or loaded as a
.zip(Adafruit provides one). - An excellent guide is available on the Arduino.cc website.
Useful functions (as on the slide):
lcd.begin(column, row); // Identifies the number of columns and rows of the display
lcd.setCursor(0, 0); // Place the cursor at a specific point (row, column)
lcd.print("Text"); // Sends the text string enclosed by " " to the display
lcd.blink(); // Activates cursor flashing
lcd.noBlink(); // De-activates cursor flashing
lcd.cursor(); // Shows the cursor
lcd.noCursor(); // Hides the cursor
lcd.display(); // Shows what is written on the display
lcd.noDisplay(); // Hides what is written on the display, preserving the previously displayed text(The slide writes lcd.print(»Text") with a stray opening guillemet; the intended form is lcd.print("Text"). The slide's comment says "(row, column)" for setCursor(0, 0); since the example uses two zeros, the order of the two arguments is not demonstrated on the slide: check it in the library reference before use.)
DHT11 + LCD
Final assembly of the lab (figure): the DHT11 module (on a base with +, out, -) is supplied with +5 V and GND from the breadboard rails, with its out pin to a digital pin of the Arduino; the LCD sits on the breadboard with the trimmer for contrast, the data/control lines wired to digital pins; the figure labels the supply rails "FuelCan GND" and "FuelCan +5Vdc" (an external 5 V source). The goal is to show the temperature and humidity measured by the DHT11 on the LCD. No sketch is given on the slides.
Versione ripasso
- Library: a set of functions that takes care of the hardware communication and simplifies the reading of sensor data. Several libraries may exist for one component: compare them, test them and check how recent they are.
- Installing a library (IDE menu Sketch -> Include library):
- Add .ZIP library: the zip downloaded from the developer (usually GitHub);
- Library manager: searches and installs the libraries the IDE recognises;
- manual: a folder with at least one
.cor.cppfile and one.hfile (plus optional readme and examples), zipped with the same name and placed in the.../librariesfolder.
- DHT11 (temperature and humidity):
| Quantity | Range | Accuracy |
|---|---|---|
| Temperature | 0 to 50 degC | degC |
| Relative humidity | 20 to 90 % | % |
- the DHT22 is more accurate;
- bare sensor pins: 1 to 5 V, 2 to a digital pin (e.g. pin 4), 3 not connected, 4 to GND; a 10 kOhm resistor as the slide shows;
- mounted modules (+, out, -) need only power, GND and the data line: check the datasheet;
- libraries: Adafruit DHT sensor library, and the Adafruit Sensor library.
- LCD, 16 columns and 2 rows (HD44780): VSS GND; VDD +5 V; V0 contrast from a trimmer divider (0-5 V); RS register select; R/W to GND (write only); E enable; DB0-DB7 data; A backlight anode, +4.2 V; K backlight cathode, GND. The backlight needs a limiting resistor. Wiring uses 4 data lines plus RS and E; pin numbers in the LiquidCrystal example on arduino.cc.
- LiquidCrystal library:
lcd.begin(column, row),lcd.setCursor(0, 0),lcd.print("Text"),lcd.blink()/lcd.noBlink(),lcd.cursor()/lcd.noCursor(),lcd.display()/lcd.noDisplay().noDisplayhides the text but keeps it in memory. Check the argument order ofsetCursorin the library reference. - Final assembly: DHT11 with its data pin on a digital pin, and the LCD with a contrast trimmer; the goal is to show temperature and humidity on the display. The slides give no sketch.
- Typical mistakes:
- forgetting the Adafruit Sensor library when using Adafruit libraries;
- connecting R/W to a signal instead of GND;
- no contrast trimmer (blank display);
- backlight without a resistor;
- wrong pin order on a mounted DHT module.