Arduino v2
Arduino v2
ARDUINO
Arduino
An open-source hardware platform based
on an Atmel AVR 8-bit microcontroller and
a C++ based IDE
Over 300000 boards have been
manufactured
Arduino Due is based on a 32-bit ARM
Cortex
Arduino IDE
Important functions
Serial.println(value);
Prints the value to the Serial Monitor on your
computer
pinMode(pin, mode);
Configures a digital pin to read (input) or write
(output) a digital value
digitalRead(pin);
Reads a digital value (HIGH or LOW) on a pin set for
input
digitalWrite(pin, value);
Writes the digital value (HIGH or LOW) to a pin set for
output
Using LEDs
void setup()
{
pinMode(77, OUTPUT);
//configure pin 77 as
output
}
// blink an LED once
void blink1()
{
digitalWrite(77,HIGH); // turn the LED on
delay(500); // wait 500 milliseconds
digitalWrite(77,LOW); // turn the LED off
delay(500); // wait 500 milliseconds
}
Measuring Temperature
const int inPin = 0; // analog pin
void loop()
{
int value = analogRead(inPin);
float millivolts = (value / 1024.0) *
3300; //3.3V analog input
float celsius = millivolts / 10; //
sensor output is 10mV per degree
Celsius
delay(1000); // wait for one second
}
Connecting LCDs
Using LCDs
#include <LiquidCrystal.h> // include the library
//constants for the number of rows and columns in
const int numRows = 2;
const int numCols = 16;
// initialize the library with the numbers of the
pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(numCols, numRows);
lcd.print("hello, world!"); // Print a message to
}
code
the LCD
interface
the LCD.
Audio output
tone(speakerPin, frequency, duration); // play the
tone
delay(duration); //wait for the tone to finish
Example
Write a program that plays an A note (440
Hz) for 1 second when a motion sensor
detects motion.
Using Interrupts
On a standard Arduino board, two pins
can be used as interrupts: pins 2 and 3.
The interrupt is enabled through the
following line:
attachInterrupt(interrupt, function, mode)
attachInterrupt(0, doEncoder, FALLING);
Interrupt example
int led = 77;
volatile int state = LOW;
void setup()
{
pinMode(led, OUTPUT);
attachInterrupt(1, blink, CHANGE);
}
void loop()
{
digitalWrite(led, state);
}
void blink()
{
state = !state;
}
Example (1/2)
#include "Timer.h"
Timer t;
int ledEvent;
void setup()
{
Serial.begin(9600);
int tickEvent = t.every(2000, doSomething);
Serial.print("2 second tick started id=");
Serial.println(tickEvent);
pinMode(13, OUTPUT);
ledEvent = t.oscillate(13, 50, HIGH);
Serial.print("LED event started id=");
Serial.println(ledEvent);
int afterEvent = t.after(10000, doAfter);
Serial.print("After event started id=");
Serial.println(afterEvent);
}
Example (2/2)
void loop()
{
t.update();
}
void doSomething()
{
Serial.print("2 second tick: millis()=");
Serial.println(millis());
}
void doAfter()
{
Serial.println("stop the led event");
t.stop(ledEvent);
t.oscillate(13, 500, HIGH, 5);
}
ChipKit MAX32
Microcontroller: PIC32MX795F512L
Flash Memory: 512K
RAM Memory: 128K
Operating Voltage: 3.3V
Operating Frequency: 80Mhz
Typical operating current: 90mA
Input Voltage (recommended): 7V to 15V
Input Voltage (maximum): 20V
I/O Pins: 83 total
Analog Inputs: 16
Analog input voltage range: 0V to 3.3V
DC Current per pin: +/-18mA
Advanced peripherals:
10/100 Ethernet MAC
USB 2.0 Full Speed OTG controller
2 CAN controllers.
External Interrupts: Pin 3 (INT0), Pin 2 (INT1), Pin 7 (INT2), Pin 21 (INT3), Pin 20
(INT4)
Temperature Sensor
A digital temperature sensor is provided using a
Microchip TCN75A 2-Wire Serial Temperature Sensor.
The temperature sensor, IC2, is an I2C device, and is
located in the lower right corner of the board.
The TCN75A is rated for an accuracy of +/-1C and has
selectable resolution from 0.5C down to 0.0625C. The
seven bit device address is 1001000.
Digilent provides a library for accessing the temperature
sensor. This library is available on the Digilent web site
and in the third party library repository on github.
Using the temperature sensor with the MAX32 board
requires manually connecting the SDA and SCL pins to
the basic I/O shield
Example
void setup(){
IOShieldTemp.config(IOSHIELDTEMP_ONESHOT |
IOSHIELDTEMP_RES11 | IOSHIELDTEMP_ALERTHIGH);
}
void loop()
{
float temp;
int celsius;
char sign, msd_char, lsd_char;
//Get Temperature in Celsius.
temp = IOShieldTemp.getTemp();
}
Potentiometer
OLED Display
128x32 pixels
Each individual pixel can only be on
(illuminated) or off (not illuminated)
The display is a serial device that is
accessed via an SPI interface.
write-only device
Example
void setup()
{
IOShieldOled.begin();
}
void loop()
{
char toprint;
IOShieldOled.clearBuffer();
IOShieldOled.setCursor(0, 0);
toprint = A;
IOShield0led.putChar(toprint);
}
INTRODUCTION TO SENSORS
What is a sensor?
A device that receives a stimulus and
responds with an electrical signal.
A special type of transducer (device that
converts one type of energy into another
Common Sensors
Mechanical
Accelerometers
Gyroscopes
Optical
Photodetectors
Infrared
Semiconductor
Gas
Temperature
Magnetic
Example
Calculate the temperature for a copper
RTD if R0 = 500 and room temperature
and a = 0.0043. The measured resistance
is 500.43
Accuracy
Transfer Function
S=F(x), where x is the measurand and S the electrical
signal (commonly Voltage)
Sensitivity
The change in input required to generate a unit
change in output
Random error
Noise
References
M.J. McGrath, C. N. Scanaill and D. Nafus,
Sensor Technologies Healthcare, Wellness and
Environmental Applications, Apress, 2013
C.W. de Silva, Sensors and Actuators: Control
System Instrumentation, 2nd Edition, CRC Press,
2015
T. Karvinen and K. Karvinen, Make: Sensors: A
Hands-On Primer for Monitoring the Real World
with Arduino and Raspberry Pi, Maker Media,
Inc, 2014
ACTUATORS
Actuators
Device that turns energy (typically
electrical) to motion
Features
Force
Speed
Torque
Power
Efficiency
DC motor
Force is produced
(F=ILB) due to the
electric current in a
wire inside a magnetic
field.
Proportional to the
current, therefore can
be controlled by
potentiometer
Hard to control
precisely
Servo motors
A DC motor with a
control circuit
Servo motors are
controlled by PWM
through the control
pin
Stepper Motors
Three modes:
Stand-alone: GPS obtains information such as position and
altitude with only the signal of the satellites
slowest of the three modes
AT+CGPSINFO command brings directly latitude, logtitude,
date, UTC time, altitude and speed
S-GPS: module connects to a GPS server
module calculates the position
A-GPS: Three modes
MS-Assisted: Server sends data and calculates position
MS-based: Server sends aiding data, module calculates
position
Stand-Alone: Module demodulates GPS data and calculates
position