3.d. Arduino
3.d. Arduino
Summary
Arduino is an open-source
electronic prototyping
platform.
Several boards have been
developed through the
years. We will consider
here the Arduino UNO.
Pinout:
• Power: 3.3 V, 5V, 3
Ground, Vin (power
input or output if power
is supplied through the
jack)
• Analog in: 6 pins,
resolution: 10 bits
• SPI interface for SPI
sensors
• 6 Pulse Width
Modulation outputs
• 2 pins for serial TX and
RX(0 and 1)
• 1 reset pin
Commands
New sketch
Serial Monitor
Verify Upload
Editor
Message window
// the setup function runs once when you press reset or power
the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
This sketch blinks a led on the pin 13. Connected to the pin 13 there is a
builtin led. When the pin is high the led is on.
The sketch is organized in two main functions: setup and loop.
• Setup runs once when the board is reset
o pinMode(pinNumber, mode) tells the pin identified by
pinNumber to work as input or output through the rest of
the sketch.
o LED_BUILTIN is a constant equal to 13
o mode can be either output or input
• Loop runs continuously.
o digitalWrite(pin, value) tells the pin to switch its level to value.
Value can be HIGH or LOW (predefined constants).
o delay (msec) puts the micro in idle mode for msec
milliseconds.
• The resistor (220 Ω) prevents a short circuit on the series resistor and LED.
when the led is on. • The pin 9 is controlled using a PWM signal
• The output of the pin 9 controls the voltage
When the button is pressed, the pin 2 switches to high. The function
digitalRead can be used to measure the voltage on pin 2. In the next
example, when the button is pressed the builtin led is switched on.
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
void setup()
{
pinMode(ledPin, OUTPUT); // LED Pin initialization
pinMode(tiltPin, INPUT); // Tsensor Pin initialization
}
void loop()
{
// Reading sensor state
tiltState = digitalRead(tiltPin);
if (tiltState == HIGH)
digitalWrite(ledPin, HIGH); // LED on
else
digitalWrite(ledPin, LOW); // LED off
}
A potentiometer is a mechanical
device that provides a varying
amount of resistance when its
shaft is turned. By passing voltage
through a potentiometer and into
an analog input, it is possible to
measure the amount of resistance
produced by a potentiometer as
an analog value.
Note: With respect to the previous
examples, the potentiometer is an
analog sensor
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter
menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and
ground.
http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/
void setup() {
Serial.begin(9600);
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
Example of circuit
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Top view of the IMU For its basic use, only the pins
VCC; GND; SCL and SDA are
used.
• VCC is the power input (either
3.3 or 5V is fine);
• GND is the ground;
• SCL
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
Serial.print("Accelerometer: ");
Serial.print("X = "); Serial.print(AcX);
Serial.print(" | Y = "); Serial.print(AcY);
Serial.print(" | Z = "); Serial.println(AcZ);
//equation for temperature in degrees C from datasheet
Serial.print("Temperature: ");
Serial.print(Tmp/340.00+36.53); Serial.println(" C ");
Serial.print("Gyroscope: ");
Serial.print("X = "); Serial.print(GyX);
Serial.print(" | Y = "); Serial.print(GyY);
Serial.print(" | Z = "); Serial.println(GyZ);
Serial.println(" ");
delay(333);
}
MPU6050 mpu;
void setup()
{
Serial.begin(115200);
Serial.println("Initialize MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
// Check settings
checkSettings();
}
//Ignore the gyro if our angular velocity does not meet our threshold
if (normGyro.ZAxis > 1 || normGyro.ZAxis < -1) {
normGyro.ZAxis /= 100;
yaw += normGyro.ZAxis;
}
// Output
Serial.print("Pitch = ");
Serial.print(pitch);
Serial.print("\tRoll = ");
Serial.print(roll);
Serial.print("\tYaw = ");
Serial.print(yaw);
Serial.println();
delay(10);
}
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT); pulseIn function: reads a pulse (either HIGH or
Serial.begin(9600);
} LOW) on a pin. For example, if value is HIGH,
pulseIn() waits for the pin to go HIGH, starts
void loop() {
digitalWrite(trigPin, LOW); timing, then waits for the pin to go LOW and
delayMicroseconds(2); stops timing.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); Returns the length of the pulse in
digitalWrite(trigPin, LOW); microseconds.
duration = pulseIn(echoPin, HIGH); Gives up and returns 0 if no pulse starts within
distance = (duration*.0343)/2; a specified time out.
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
pirValue = digitalRead(pirPin);
digitalWrite(ledPin, pirValue);
(
~charArray = [ ];
~getValues = Routine.new(
{ var ascii;
{ascii = ~port.read.asAscii;
if(ascii.isDecDigit,{~charArray =
~charArray.add(ascii)});
if(ascii == $a,{
~val1=
~charArray.collect(_.digit).convertDigits
;
~charArray = [ ];
});
}.loop;}
).play;)
void loop() { }
Synth definition:
(
SynthDef.new("theremin_ultrasonic",{arg
fund_freq=440;
var sig = Saw.ar(fund_freq,0.2,0);
Out.ar(0,Pan2.ar(sig,0,1))}).send(s);
)
~t =
Synth("theremin_ultrasonic",["fund_freq",~val1.l
inlin(0,90,110,880)]);
The method linlin maps linearly the values in ~val1 in the range
[0,90] to the output range [110,880]. Similarly, the linexp method
performs an exponential mapping.
Arduino script {
if (sensorValue1 < sensorLow)
int sensorValue1 = 0;
sensorLow = sensorValue1;
int sensorValue2 = 0;
}
int sensorLow = 1023;
}
int sensorHigh = 0;
}
int temp;
void loop() {
void setup() {
temp = analogRead(A5);
Serial.begin(9600);
sensorValue1 = temp;
sensorValue2 = analogRead(A4);
while(millis()<5000)
Serial.print(temp);
{
Serial.print("a");
sensorValue1 = analogRead(A5);
Serial.print(sensorValue2);
if (sensorValue1 > sensorHigh)
{ Serial.print("b");
sensorHigh = sensorValue1; delay(10);
} }
// record the minimum sensor
value
(~charArray = [ ];
~getValues = Routine.new(
{ var ascii;
{ ascii = ~port.read.asAscii;
if(ascii.isDecDigit,{~charArray =
~charArray.add(ascii)});
if(ascii == $a,{
~val1= ~charArray.collect(_.digit).convertDigits;
~charArray = [ ]; });
if(ascii == $b,{
~val2 =
~charArray.collect(_.digit).convertDigits;~charArr
ay = [ ]; });
}.loop;}
).play;
)
(SynthDef.new("signal",
{
arg fundamental = 200,pan = 0;
var harmonics = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var sig = VarSaw.ar(LFPulse.kr(3, 0, 0.3,
fundamental,
fundamental*1.5),0,LFTri.kr(1.0).range(0,1),
0.1); Out.ar(0,Pan2.ar(sig,pan));}).add;)
~synth = Synth("signal");
(~control = Routine.new({ {
~synth.set(\fundamental,~val1.linexp(0,200,11
0,880,880),\pan,~val2.linlin(0,1023,-1,1,1));
0.05.wait;
}.loop;}
).play;
)