Lab 10 - Arduino Standalone and Serial Protocols
In questa pagina 3
Continuation of Lab 9 - Arduino Libraries and Sensor IntegrationLaboratory 9 shows how to install and write Arduino libraries, connect the DHT11 temperature and humidity sensor, wire a 16-pin HD44780 LCD with a contrast trimmer, drive it with the LiquidCrystal library and combine sensor and display.Lab 9 - Arduino Libraries and Sensor Integration → 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 →: here the ATMega328P chip is used alone (see the block diagram in 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 →).
Why use only the microcontroller chip
Reasons for using the chip instead of the whole Arduino board:
- Shape and size: ergonomic, aesthetic or space criteria, or project specifications that do not match the board's dimensions.
- Interconnections: power supply and I/O pins can be used directly, without soldering that reduces the quality of the contact.
- Cost: only the necessary components are used (approximately 4-5 euros, even less).
Programming a standalone chip
- The Arduino bootloader, like many other boards, uses an FTDI converter (a USB -> serial converter; FTDI is the name of a Scottish company that makes such chips) for communication.
- To talk to the microcontroller we therefore need either an Arduino board from which the microprocessor has been removed (the board is used as a USB-serial adapter) or a USB-FTDI conversion board.
- Signals needed: Vcc (+5 V) and GND for power, RX and TX for serial communication, and a pin connected to reset.
Required hardware (not mandatory)
- 1 external 16 MHz quartz oscillator;
- 2 22 pF capacitors;
- 1 10 kOhm resistor (on the reset line in the figure);
- 1 ATMega328P microcontroller (better if the bootloader is already installed).
In the breadboard figure the microcontroller is removed from the Arduino UNO socket; the chip sits on the breadboard with the crystal between pins 9 and 10, each crystal terminal connected to ground through a 22 pF capacitor, and the 10 kOhm resistor from reset to 5 V.
Pin map of the 28-pin ATMega328P (as on the slides)
| Pin | Function |
|---|---|
| 1 | RESET: resets the ATMega328P |
| 2-6 | digital I/O 0 to 4 (in the pin figure: pin 2 = digital 0 / rx, pin 3 = digital 1 / tx, pin 4 = digital 2, pin 5 = digital 3 pwm, pin 6 = digital 4) |
| 7 | Vcc, +5 V |
| 8 | GND |
| 9 and 10 | XTAL: connection of the 16 MHz quartz oscillator |
| 11-19 | digital I/O 5 to 13 |
| 20 | Avcc: analog supply (the text says "analog reference, external reference voltage for the analog inputs"); in the minimal circuit it goes to +5 V |
| 21 | Aref (text: "Vcc +5 V"; in the pin figure: "not connected") |
| 22 | GND |
| 23-28 | analog inputs (23 = analog 0, 28 = analog 5) |
The pin figure on the slide also shows the PWM pins of the package: digital 3 (pin 5), 5 (pin 11), 6 (pin 12), 9 (pin 15), 10 (pin 16), 11 (pin 17). The minimal circuit drawn on the slide has: +5 V on pins 1 (via the reset pull-up), 7 and 20; GND on pins 8 and 22; the 16 MHz crystal between pins 9 and 10 with a capacitor from each terminal to ground; RX and TX brought out on pins 2 and 3.
Uploading the bootloader to a "virgin" microcontroller
Steps from the slide:
- Upload on the Arduino the sketch Examples -> ArduinoISP -> ArduinoISP (the Arduino becomes the programmer).
- Connect the reset of the microcontroller to pin 10 of the Arduino, instead of to the board reset.
- Connect pins 11-13 (to the corresponding chip pins: the SPI lines MOSI, MISO, SCK of the programmer; see SPI below).
- TX and RX are not used.
- Insert a 10 uF electrolytic capacitor between the 3.3 V pin (+) and the Reset pin (-) (on the Arduino board).
- In the IDE select Tools -> Programmer -> Arduino as ISP.
- In the IDE select Tools -> Write the bootloader.
Using the internal clock instead of the quartz
- With the microcontroller's internal clock there are no extra electronic components, but an additional hardware configuration must be downloaded.
- Download
breadboard-1-6-x.zipfrom the arduino.cc site (address on the slide: https://www.arduino.cc/en/uploads/Tutorial/breadboard-1-6-x.zip). - Put the downloaded zip in the
.../Documents/Arduinodirectory, inside a folder to be created, named hardware. - Select ATmega328 on a breadboard (8 MHz internal clock) in the Tools -> Board menu (it was not available before).
First test
- Load an IDE example sketch, e.g. ReadAnalogVoltage or AnalogReadSerial (see 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 →), and check what happens: connect the chosen analog pin to +5 V, +3.3 V or GND and see whether the corresponding value is received.
- The serial plotter (Tools menu) can be used instead of the serial monitor to display a graph; the slide shows an example with two sinusoids and suggests an LM35 temperature sensor as an example (the plot window reads 115200 baud).
Troubleshooting
- Check and recheck the connections (it is very easy to make a mistake between pins and components).
- Check the connection wires with a continuity test with the multimeter (see Electrical Measurements - Voltmeter and MultimeterAnalog and electronic instruments (voltmeter, ammeter, ohmmeter, wattmeter) compare the measurand with the unit; the loading effect is a systematic error predicted by voltage and current dividers, and digital multimeters are specified by digits, resolution, input impedance and an accuracy of the form k1 times reading plus k2 times range.Electrical Measurements - Voltmeter and Multimeter →).
- Check that the right components are used (resistor, quartz, etc.).
- Check that the right microcontroller is used: ATMega328P-PU is the version with the bootloader already preloaded.
- Check that the board left without its microcontroller is not damaged (possibly put the micro back in the socket and try to upload any sketch).
- In the IDE select the correct type of micro and communication port, as usual.
Serial protocols
- Communication between Arduino and other devices can use serial protocols such as I2C, SPI, Modbus, CAN bus, RS232.
- This simplifies the work of the master board, since complex processing functions are delegated to a slave unit.
- Synchronous serial interfaces have a synchronization signal (clock, generated by the master) that sets the pace at which RX/TX data pass on the bus.
- Advantage: all devices involved can communicate independently of their local oscillator, so peripherals operating at very different frequencies can be interfaced.
- Two synchronous serial interfaces: SPI (Serial Peripheral Interface, developed by Motorola) and I2C (Inter Integrated Circuits, developed by Philips).
I2C
- Based on two wires: SDA (data) and SCL (clock), on pins A4 and A5 of the Arduino UNO (see the pinout in 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 →).
- Figure: one Arduino master drives the shared DATA (SDA) and CLOCK (SCL) lines, to which the SDA and SCL pins of several Arduino slaves are all connected in parallel.
- The data exchange is managed by the master through the unique addresses of the slaves (assigned in the code). The architecture can also be multi-master.
- Data flow:
- the master sends a start bit on the bus;
- the master sends the address of the slave it wants to talk to;
- the master decides whether to write or read;
- the slave reads or writes according to the master's request.
- The Wire library has all the functions needed for the master-slave connection between two Arduino boards.
Exercise (figure: an Arduino UNO and an ATMEGA328P-PU on a second board with 10 kOhm reset resistor, 16 MHz crystal and two 18 pF capacitors to ground, joined on A4/A5 with 5 V, GND and reset; two resistors labelled R appear on the SDA/SCL lines in the figure, their value is not given):
- implement the architecture shown and test the Wire library examples;
- test the communication between the Arduino and the ATMega328P.
SPI
- A master communicates with several slave peripherals.
- Number of connections: 3 + n wires, where n is the number of slave peripherals.
- Signals:
- SCLK (Serial shift CLocK): synchronization signal generated by the master, reaching all slaves;
- MOSI (Master Out Slave In): data line leaving the master, reaching all the slaves in parallel;
- MISO (Master In Slave Out): data line leaving each slave and reaching the master;
- SS (Slave Select): one selection line per slave, active LOW (a logic LOW on /SSn activates device n).
- Having separate data lines allows full-duplex communication.
- Figure: master "Microcontrollore" with lines SCLK, MOSI, MISO shared by SLAVE 1, 2, 3, and three separate lines /SS1, /SS2, /SS3, one for each slave.
Comparison (summarizing the two descriptions above):
| I2C | SPI | |
|---|---|---|
| Wires | 2 (SDA, SCL) | 3 + n (SCLK, MOSI, MISO plus one SS per slave) |
| Slave selection | unique address sent by the master | /SS line pulled LOW |
| Clock | generated by the master | generated by the master (SCLK) |
| Duplex | (not stated on the slide) | full-duplex |
| Developed by | Philips | Motorola |
Versione ripasso
- Why only the chip (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 → has the block diagram): shape and size constraints; power and I/O pins used directly, without soldering; cost about 4-5 euro or less.
- Programming: the bootloader talks through an FTDI (USB to serial) converter. Use either an Arduino with its microprocessor removed, or a USB-FTDI board. Needed signals: Vcc +5 V, GND, RX, TX and reset.
- Required hardware: 16 MHz quartz, two 22 pF capacitors (one on each crystal terminal to ground), a 10 kOhm resistor on reset, the ATMega328P (ATMega328P-PU already has the bootloader).
- 28-pin ATMega328P map:
- 1 RESET; 7 Vcc (+5 V); 8 GND; 9-10 XTAL (16 MHz quartz); 20 Avcc; 21 Aref; 22 GND;
- 2-6 digital 0-4 (2 = RX, 3 = TX); 11-19 digital 5-13; 23-28 analog 0-5;
- PWM on digital 3, 5, 6, 9, 10, 11 (package pins 5, 11, 12, 15, 16, 17).
- Bootloader on a virgin chip:
- upload Examples -> ArduinoISP -> ArduinoISP to the Arduino;
- chip reset to Arduino pin 10; chip pins 11-13 to the Arduino SPI lines (MOSI, MISO, SCK); TX and RX not used;
- 10 uF electrolytic capacitor between the 3.3 V pin (+) and reset (-);
- Tools -> Programmer -> Arduino as ISP; then Tools -> Write the bootloader.
- Internal clock option (no quartz): download breadboard-1-6-x.zip, unpack it in
Documents/Arduino/hardware, select "ATmega328 on a breadboard (8 MHz internal clock)". - First test: ReadAnalogVoltage or AnalogReadSerial (see 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 →), with the analog pin tied to +5 V, +3.3 V or GND; the serial plotter can show a graph (LM35 suggested, 115200 baud).
- Troubleshooting: re-check pins and components; check each wire with the multimeter continuity test (Electrical Measurements - Voltmeter and MultimeterAnalog and electronic instruments (voltmeter, ammeter, ohmmeter, wattmeter) compare the measurand with the unit; the loading effect is a systematic error predicted by voltage and current dividers, and digital multimeters are specified by digits, resolution, input impedance and an accuracy of the form k1 times reading plus k2 times range.Electrical Measurements - Voltmeter and Multimeter →); check the chip version and the board and port in the IDE.
- Serial protocols: I2C, SPI, Modbus, CAN bus, RS232. Synchronous ones send a clock generated by the master; devices work independently of their local oscillators. The master delegates processing to slaves.
- I2C (Philips): two wires, SDA and SCL (A4 and A5 on the UNO).
- Data flow: start bit; slave address (set in the code); write or read bit; the slave answers.
- Multi-master possible;
Wirelibrary.
- SPI (Motorola): 3 + n wires.
- SCLK (clock from master, all slaves), MOSI (master to slaves, parallel), MISO (slaves to master), one /SS per slave, active LOW.
- Separate data lines give full duplex.
- Typical mistakes:
- leaving the micro on the UNO when it is used as USB-serial adapter;
- forgetting the reset capacitor;
- selecting the 8 MHz internal board instead of the 16 MHz quartz setup, or the opposite;
- using a chip without bootloader (the -PU version has it).