It covers several topics: basics, drivers and shields, GPIO, compilers and IDEs, hradware protocols I2C, SPI, UART. The presentation also provides several examples: DC motor control, stepper motor control, reading analog data ADC, what is DMA and a music equalizer (both software and hardware).
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
How to use an Arduino
1. Grenoble | images | parole | signal | automatique | laboratoire
UMR 5216
Anton Andreev
07/05/2020
INTRODUCTION TO
ARDUINO
2. Contents
INTRODUCTION TO ARDUINO
1. General information
- Specification
- Drivers and Shields
- GPIO
- Compilers and IDEs
- Hardware protocols: I2C, SPI, UART
2. Examples
- DC Motor control
- Stepper motor control
- Reading analog data ADC
- What is DMA optimization?
- Equalizer in software and hardware
3. In a nutshell
INTRODUCTION TO ARDUINO
Arduino is:
• Board + Microcontroller unit (MCU)
• A standard (Arduino comtible/clone)
Arduino’s features:
- 8 bit 32 registers RISC processor, AVR family
- Produced by Atmel (acquired by Microchip Technology)
- In-system programmer
- Bootloader
- Analog to Digital Converter (ADC)
- 10-bit ADC with 1,024 (2^10) discrete analog levels
- 14 input/output pins
- Comes with IDE (Integrated Development Environment)
- Many libraries, examples and documentation
4. Specification
INTRODUCTION TO ARDUINO
- 8 bit AVR CPU
- Flash Memory is 32 KB
- SRAM is 2 KB (RAM)
- EEPROM is 1 KB
- CLK Speed is 16-20 MHz
- Some versions have DMA
- 32bit ARM CPU
- Flash Memory is 512 KB
- SRAM is 96 KB
- EEPROM is 1 KB
- CLK Speed is 74-80 MHz
- Has RTC
- Has DMA
Arduino Arduino clone STM32 Nucleo
Many clones of Arduino exist often with better hardware parameters,
Bluetooth and WIFI modules built-in
Arduino also has several versions: UNO, Leonardo, Mega, Ethernet
5. Drivers and Shields
INTRODUCTION TO ARDUINO
Example: the L298N first as a driver
and then a shield
Difference:
- A motor driver is a chip that drives motors. A motor shield is a circuit
board with connections on it that contains a motor driver chip that
drives motors. A shield is convenient since you can just plug it in to your
Arduino and wire the motors direct to it, but it lacks the flexibility of a
raw driver chip which you can wire up precisely as your project
demands.
Other popular shields for Arduino: ethernet, WIFI, RGB LCD
6. Arduino vs Raspberry Pi Short Comparison
INTRODUCTION TO ARDUINO
Pi is more powerful, like a normal computer
Raspberry Pi is:
- Multicore ARM 1.2Ghz,integrated GPU, RAM 1GB
- Has GPIO 40 pins compared to Arduino’s 20
- 8 channel 17 bit ADC
Usage:
- For higher level tasks
- Machine Learning (any ML library that compiles on
Linux OpenCV included), Image Processing
- Multimedia
- Small factor computers
- Surveillance systems
- Draws more energy than Arduino
- More expensive
- Has no storage by default
Arduino can be used as a
slave for Raspberry Pi
7. Compilers
INTRODUCTION TO ARDUINO
Arduino
program/sketch
Small modifications to
produce standard C code
Every manufacturer has its own software
development toolchain
Avr-g++ compiler is part of Atmel AVR GNU
Toolchain. It is based on GNU GCC and
tools created by AVR
Code for STM32 is pure C/C++ with libraries
from ST. The compiler is part of the
STM32Cube Ecosystem. The compiler is
called GNU Tools for STM32 and it based
on a patched version GNU Arm Embedded
Toolchain avr-g++
compiler
8. CircuitPython
INTRODUCTION TO ARDUINO
- Fork of MicroPython
- Targets: low memory and performance boards
- 32 bit ARM (such as STM32 or ESP8266)
- But not the original Arduino with 8 bit AVR CPU
- Python 3 compiler and Virtual Machine are on the board
- REPL on the USB through serial
- Controller’s flash memory is exposed as USB thumb drive
- .py files can be copied onto the board and executed
- main.py or code.py are executed automatically on restart
- Can restart automatically when .py file has changed
- Result of “print” command is visible in the Serial Monitor
- Can have only the VM and bytecode compiled program on the board
9. IDEs
INTRODUCTION TO ARDUINO
- Arduino IDE
- Programs are called sketches
- Easy program upload
- Easy configuration
- Serial Port
- Arduino type or clone
- Atom IDE + Platformio plugin
- Visual Studio with Arduino add-on
- Paid
- Integrated debugging
- Eclipse for Arduino
- SMT32 System Workbench (only for STM32)
- STM32CubeIDE (only for STM32)
10. Arduino GPIO
INTRODUCTION TO ARDUINO
General-Purpose Input Output (GPIO)
14 digital and 6 analog pins ca be used
for both input and output
pinMode (3, OUTPUT) //set as output
digitalWrite (3, HIGH) //set 3,3V value
digitalRead(pin) //read state
digitalRead(pin) // ADC 5v -> [0..1023]
12. Digital Input with Pull-up
Resistor
INTRODUCTION TO ARDUINO
Problem:
Sometimes switching between one state
to another or pins configured as input with
nothing connected to them may arise
situation of High-impedance state i.e.
floating state. This state may report
random changes in pin state
Solution:
Adding pull-up (to +5V) or pull-down (to
Gnd) resistor which helps to set the input
to a known state
pinMode (3, INPUT_PULLUP)
Pull Down resistor
13. SPI and I2C
INTRODUCTION TO ARDUINO
When we connect a microcontroller to a
sensor, display, or other module these
device needs to communicate
Two popular hardware communication
protocols:
- SPI is used for SD cards, RFID card
readers …
- I2C is used for OLED displays,
barometric pressure sensors, or
gyroscope/accelerometer …
- Both use clocks, are serial and async
- 2 (I2C) or 4 (SPI) wires
- One or multiple masters (I2C)
- One or multiple slaves (both)
- I2C uses messages with addresses to
communicate with salves, SPI use selects
to which slave to speak
- Speed of transfer (SPI is faster)
I2C message
I2C: SDA (Serial Data) and SDL (Serial Clock)
14. I2C OLED example
INTRODUCTION TO ARDUINO
#include <Arduino.h>
#include <U8g2lib.h> //library for control of the OLED screen
#include <SPI.h>
#include <Wire.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
void setup(void)
{
u8g2.begin();
}
void loop(void)
{
u8g2.clearBuffer(); //clear the internal memory
u8g2.setFont(u8g2_font_logisoso28_tr);
u8g2.drawStr(8,29,“GIPSALAB"); //write something to the internal
memory
u8g2.sendBuffer();//transfer internal memory to the display
}
On the Arduino:
Pin 19 blue line is SCL
Pin 18 purple is SCA
15. UART
INTRODUCTION TO ARDUINO
UART is used over a computer or peripheral
device serial port (COM1,COM2…) through a
UART chip
Characteristics:
- Well documented and widely used
- Two wires
- 9 bit packet (0 or 1 parity bits)
- Serial
- No clock
- Has a parity bit to allow for error checking
- No multiple slaves, only one on one
16. PWM (1)
INTRODUCTION TO ARDUINO
PWM is a technique to
use digital values and
delays to emulate
analog values
17. DC motor control With Arduino (1)
INTRODUCTION TO ARDUINO
In red the L298N
motor driver
18. DC motor control With Arduino (2)
INTRODUCTION TO ARDUINO
L298N H-bridge can used for two motors and to change in which direction
the motor spins
19. DC motor control with Arduino (3)
INTRODUCTION TO ARDUINO
We use PWM in the
range of [0 255]. So if
want 3V out of 12V
maximum then we
need to send the
value of 255/4 on the
control pin
255 corresponds to 12 volts
20. DC motor control with Arduino (4)
INTRODUCTION TO ARDUINO
int rotDirection = 0;
int pressed = false;
#define button 4
void loop()
{
// Read potentiometer value
int potValue = analogRead(A0);
// Map the potentiometer value from 0 to 255
int pwmOutput = map(potValue, 0, 1023, 0 , 255);
// Send PWM signal to L298N Enable pin
analogWrite(enA, pwmOutput);
// Read button
if (digitalRead(button) == true)
{
pressed = !pressed;
}
//wait until button is released on a single push
while (digitalRead(button) == true);
delay(20);
//If button is pressed - change rotation direction
if (pressed == true & rotDirection == 0)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
rotDirection = 1;
delay(20);
}
// If button is pressed - change rotation direction
if (pressed == false & rotDirection == 1)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
rotDirection = 0;
delay(20);
}
} //end of loop()
21. Stepper motor control with Arduino (1)
INTRODUCTION TO ARDUINO
Stepper motors can be:
• unipolar -> U2004 Darlington Array
• Bipolar -> SN754410ne H-Bridge
Also the wiring is different for Darlington Array and the H-Bridge
22. Stepper motor control with Arduino (2)
INTRODUCTION TO ARDUINO
Code is the same for unipolar and bipolar motors.
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
void setup() { // nothing to do inside the setup}
void loop()
{ // read the sensor value: int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100); // set the motor speed:
if (motorSpeed > 0)
{
myStepper.setSpeed(motorSpeed); // how many RPMs per minute
myStepper.step(stepsPerRevolution / 100); //2 steps
}
}
23. Reading analog data example
INTRODUCTION TO ARDUINO
// the setup routine runs once when you press reset:
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop()
{ // read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue); //data is send over UART
delay(1); // delay in between reads for stability
}
Reads an analog input on pin 0, prints the result to the Serial Monitor
24. DMA – Direct Memory Access
INTRODUCTION TO ARDUINO
If we need Audio processing, oscilloscope or EEG device
Arduino Zero or STM32 have built-in DMA controllers
25. Optimization with DMA
INTRODUCTION TO ARDUINO
Processing (ADC + data forwarding to UART) takes 9.6 μs
(microsecond) without DMA and 2 μs with DMA. That makes 4
times the difference.
26. Signal Processing on Arduino
INTRODUCTION TO ARDUINO
- Do we need to do it in real-time?
- Are there space and energy constraints (flying air sensor)?
- Is the ADC in Arduino good enough?
- 10 bit ADC => 1,024 (2^10) discrete analog levels
- ~1 KHz real world sampling / 9.6 KHz theoretical
- RAM limit (2KB – 96 KB)
- Is there enough libraries?
- FIR: https://www.arduinolibraries.info/libraries/fir-filter
- FFT: https://www.arduinolibraries.info/libraries/arduino-fft
- Can we augment the Arduino to do better DSP?
27. 7 bin Equalizer (1)
INTRODUCTION TO ARDUINO
Source: https://github.com/debsahu/ESP32_FFT_Audio_LEDs
Using an Arduino clone such as: ESP32 or Lolin D32
28. 7 bin Equalizer (2)
INTRODUCTION TO ARDUINO
Using software implemented FFT
- We can choose each bin frequency
- FFT https://github.com/kosme/arduinoFFT
- Bins to LED conversion:
https://github.com/G6EJD/ESP32-8266-Audio-Spectrum-Display
29. 7 bin Equalizer (3)
INTRODUCTION TO ARDUINO
- Using hardware FFT IC MSGEQ7
- Predefined bins
- The Arduino microcontroller is relieved (FFT very CPU intensive)
30. Refernces
INTRODUCTION TO ARDUINO
NUCLEO DMA:
https://www.digikey.fr/en/maker/projects/getting-started-with-stm32-
working-with-adc-and-dma/f5009db3a3ed4370acaf545a3370c30c
https://www.youtube.com/watch?v=EsZLgqhqfO0
Arduino ZERO DMA:
https://github.com/manitou48/ZERO
DC motor control:
https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-
control-tutorial-l298n-pwm-h-bridge/
Stepper motor control:
https://www.arduino.cc/en/tutorial/stepperSpeedControl
Support for STM32 in Arduino:
https://github.com/rogerclarkmelbourne/Arduino_STM32
Editor's Notes
A standard means that clones can be produced.
A standard means that clones can be produced.
In-system programmer = we do not need to remove MCU from the board
own bootloader = MCU what to load or do next. The bootloader also aids communication between the Arduino IDE and Arduino Board using USB. The bootloader is responsible for writing code on the chip as it arrives from the Arduino IDE.
Why 8 bit processor ?
SRAM is RAM for program execution
EEPROM is persistent memory. It can be used for storing small amounts of information e.x. configuration settings, passwords.
Using the EEPROM is easy with a built-in library: https://www.arduino.cc/en/Reference/EEPROM
Some Arduino clones require a driver for USB connectivity such as: CH340
MicroPython is a full Python compiler and runtime that runs on the micro-controller hardware. The user is presented with an interactive prompt (the REPL) to execute supported commands immediately. Included are a selection of core Python libraries; MicroPython includes modules which give the programmer access to low-level hardware.
Circuit Python
https://learn.adafruit.com/welcome-to-circuitpython/what-is-circuitpython
https://www.youtube.com/watch?v=rN7kvWuAt7k
STM32 in Arduino IDE: https://github.com/stm32duino
https://www.electronicwings.com/arduino/digital-gpio-of-arduino
Arduino pin are set as digital input (default).
GPIO 20 (6 analog + 16
There are only two external interrupt pins on the ATmega168/328 (ie, in the Arduino Uno/Nano/Duemilanove), INT0 and INT1, and they are mapped to Arduino pins 2 and 3. These interrupts can be set to trigger on RISING or FALLING signal edges
https://www.electronicwings.com/arduino/digital-gpio-of-arduino
Arduino pin are set as digital input (default).
I²C (Inter-Integrated Circuit), pronounced I-squared-C
https://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/
https://www.circuitbasics.com/basics-of-the-spi-communication-protocol
SPI, I2C, and UART are quite a bit slower than protocols like USB, ethernet, Bluetooth, and WiFi, but they’re a lot more simple and use less hardware and system resources. SPI, I2C, and UART are ideal for communication between microcontrollers and between microcontrollers and sensors where large amounts of high speed data don’t need to be transferred.
It can be difficult to choose between servo motors and stepper motors as there are so many considerations: cost, torque, efficiency, speed, circuitry and more.
Let’s take the a simple DC motor.
The L298N main function is to change direction of current, so that we can change the direction of rotation of the motor or simply said if we are going forward or backward. It supports two motors.
if (digitalRead(button) == true) checks if the button is pressed
while (digitalRead(button) == true); waits until the button is released
“Pressed” is not if the button is currently pressed, but it the requested direction.
Pressed = requested direction 1
Not pressed == requested direction 0
We need to know the number of steps per revolution.
We need how many steps of the motor to perform how fast each step is performed.
We always advance 2 steps. We control the speed in RPMs. Higher RPMS means that it takes less time a single step to be performed. After each two steps we check if there is a request to increase the speed per step.
A microsecond is equal to 1000 nanoseconds or 1⁄1,000 of a millisecond.
For a 16 MHz Arduino the ADC clock is set to 16 MHz/128 = 125 KHz. Each conversion in AVR takes 13 ADC clocks so 125 KHz /13 = 9615 Hz. 9615 Hz is the theoretical maximum.
Source: https://arduino.stackexchange.com/questions/699/how-do-i-know-the-sampling-frequency
ADCs can vary greatly between microcontroller. The ADC on the Arduino is a 10-bit ADC meaning it has the ability to detect 1,024 (2^10) discrete analog levels. Some microcontrollers have 8-bit ADCs (2^8 = 256 discrete levels) and some have 16-bit ADCs (2^16 = 65,536 discrete levels).
Source: https://learn.sparkfun.com/tutorials/analog-to-digital-conversion/all
https://www.youtube.com/watch?v=8YaeUYZ_Ex8
Note on Arduino clones:
Both chips have a 32-bit processor. The ESP32 is dual core 160MHz to 240MHz CPU whereas the ESP8266 is a single core processor that runs at 80MHz.
The ESP32 is the ESP8266 successor. It adds an extra CPU core, faster Wi-Fi, more GPIOs, and supports Bluetooth 4.2 and Bluetooth low energy. Additionally, the ESP32 comes with touch sensitive pins that can be used to wake up the ESP32 from deep sleep, a built-in hall effect sensor and a built-in temperature sensor.
Spécifications de la Wemos Lolin D32 Pro
based on ESP32
requires driver CH340 for USB connectivity
SoC : ESP32-WROVER @ 240MHz avec 4Mo de mémoire 4 Mo et 4Mo de PSRAM
Connectivités
WiFi 802.11 b/g/n
Bluetooth 4.1 LE
Stockage : emplacement pour carte micro SD prenant en charge le mode SPI. La capacité maximale n’est pas précisée
Connecteurs
1x JST SH 1.0mm 10 broches pour écran TFT