Salta al contenuto
Note per Studenti Lab 10 - Arduino Standalone and Serial Protocols Report

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:

  1. Upload on the Arduino the sketch Examples -> ArduinoISP -> ArduinoISP (the Arduino becomes the programmer).
  2. Connect the reset of the microcontroller to pin 10 of the Arduino, instead of to the board reset.
  3. Connect pins 11-13 (to the corresponding chip pins: the SPI lines MOSI, MISO, SCK of the programmer; see SPI below).
  4. TX and RX are not used.
  5. Insert a 10 uF electrolytic capacitor between the 3.3 V pin (+) and the Reset pin (-) (on the Arduino board).
  6. In the IDE select Tools -> Programmer -> Arduino as ISP.
  7. 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.zip from 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/Arduino directory, 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

Troubleshooting

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

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

Teoria collegata