Using Arduino Workshop

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

MassBay Community College

ET 111 - iCREAT I

Workshop

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd


This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
MassBay Community College
ET 111 - iCREAT I

Electronic Components 1:
Breadboard, Batteries, Resistors, Ohm’s law, LEDs

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
Breadboard and Jumper Wires

a board for making an experimental model of an


electric circuit. You can read more here

Jumper Wires
Battery
Batteries have three parts, an anode
(-), a cathode (+), and the
electrolyte. The cathode and anode
(the positive and negative sides at
either end of a traditional battery)
are hooked up to an electrical
circuit. The chemical reactions in
the battery causes a buildup of
electrons at the anode. Based on materials from NCWIT.org/etextiles
LED
A light-emitting diode (LED) is a
two-lead semiconductor light source. It
is a p–n junction diode, which emits light
when activated. When a suitable voltage
is applied to the leads, electrons are able
to recombine with electron holes within
the device, releasing energy in the form
of photons. You can read more here
20 mA is a good MAXIMUM current rating
Ohm’s law

You can calculate the current using


Ohm's Law: I = V / R where
I is the current, in AMPS
V is the voltage in volts
R is the resistor value, in ohms.
To learn more about the specification,
go here Based on materials from NCWIT.org/etextiles
LED Circuit

R = V/I
V = 9V
I = 20mA (.02)
Max

R=9V / 20mA

R=450 Max
What is a Resistor?

a device having a designed


resistance to the passage of
an electric current You can
learn more here.
Ω Ω

Ω
MassBay Community College
ET 111 - iCREAT I

TinkerCAD
Simulate Your Circuit First

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
Starting TinkerCAD

Go to https://www.tinkercad.com/
Create an account and sign up
When you are ready select Circuits
Create new circuit
How Tinkercad Works

● A name will be provided


to your circuit
automatically
● Select the components
for your circuit:
○ Arduino Uno
○ Breadboard
○ LED
○ resistor , etc.
● Drag them to the work
area one by one
● Rotate and move them if
necessary
● Connect them
MassBay Community College
ET 111 - iCREAT I

Practice Lab 1
Do Part 2 only
1. Simulate in TinkerCAD
2. Implement Physical Design

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
MassBay Community College
ET 111 - iCREAT I

Arduino
Arduino, Arduino IDE

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
What is the Arduino?

An Arduino is a
microcontroller that can be
programmed to do many
kinds of things! You can learn
more here.
Programming the Arduino

To program the Arduino board we will use the


Arduino Integrated Development Environment (IDE)
We will also use the TinkerCAD blocks simulation
tool to help us code initially
To start the IDE look for the this icon on your
desktop
Example of coding and simulating with Tinkercad

Use the code editor


blocks to start coding

Start the simulation and


upload and run your
code.

Copy the code into the


Drag blocks Arduino IDE once all
works well.

Blocks and text code will be synchronized. STOP using blocks once
you are familiar with the commands
Connect your Arduino to your computer

Use the USB cable to connect


your computer to the Arduino
board.
Important Pins in the Arduino Microcontroller
Internal LED on Pin 13. Use it
to test components! Use these digital pins for your LEDs and push button. You can send or read
a HIGH or LOW value only. DIGITAL!

Use these analog pins for your LDR or temperature sensors,


potentiometer. You will read values between 0 and 1023. ANALOG!
Make sure your circuits are closed! Do not
forget to connect components to GROUND!
Starting the Arduino IDE

There may be a lot of boards listed, and


multiple Arduinos. Make sure to choose the
correct board.
If you have trouble figuring out what the serial
port is for your board, unplug the board, then
go back to Tools > Port: and check the list to
see what disappears/reappears when you
plug in the board.
Terminology

For more on terminology check out the programming cheat sheet


Making an LED Blink (Review)

Turn pin 13 ON.


Stop program for 1 second.
Turn pin 13 OFF.
Stop program for 1 second.
MassBay Community College
ET 111 - iCREAT I

Practice Lab 2
Do Part 1 only
1. Simulate in TinkerCAD
2. Implement Physical Design

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
MassBay Community College
ET 111 - iCREAT I

Buzzer

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
Buzzer
A buzzer or beeper is an audio
signalling device, which may be
mechanical, electromechanical, or
piezoelectric. Typical uses of
buzzers and beepers include
alarm devices, timers, and
confirmation of user input such as
a mouse click or keystroke.
Based on materials from NCWIT.org/etextiles
Understanding Sound

Based on materials from NCWIT.org/etextiles


The Tone Procedure

For more info watch: Using the Arduino tone() function with a piezo speaker
Based on materials from NCWIT.org/etextiles
Notes and Frequencies

28
Based on materials from NCWIT.org/etextiles
MassBay Community College
ET 111 - iCREAT I

Practice Lab 3
Do Part 3 only
1. Simulate in TinkerCAD
2. Implement Physical Design

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
MassBay Community College
ET 111 - iCREAT I

Electronic Components 2:
Breadboard, Batteries, Resistors, Ohm’s law, LEDs

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
LDR-light dependent resistor
A photoresistor (or light-dependent
resistor, LDR, or photocell) is a
light-controlled variable resistor.
The resistance of a photoresistor
decreases with increasing incident
light intensity; in other words, it
exhibits photoconductivity. You can
read more here
Based on materials from NCWIT.org/etextiles
Analog Input: Reading Sensors

Connect a light sensor to an Analog Pin and


check the data being received!
They will send a value between 0 and 1023.
Display or test the Analog pin!

Analog Input will only work with the analog pins on the Arduino. Use
an If/Else block to test the data received.
Displaying the Data Read

We know the sensor is sending data through the pin. It would be


nice to see the data. Right?
Let’s ask the Arduino to print it on the screen so we can see it.
Print to the Serial Monitor
Using an IF/ELSE block to make decisions

Ask a question. This is the test statement

If the answer is TRUE, then run code here


If the answer is FALSE, then run code here

Use Comparison Less than


Operator blocks Less or equal
to test values in Equal
Not equal
the question Greater than
Greater or equal

MassBay - Susanne Steiger-Escobar


Based on materials from Sparkfun – Angela Sheehan
Open Serial Monitor and Check the data

Open Serial Monitor


Airplane Sample Code

void loop() {
int LEDright = 9; // Right LED Serial.println(analogRead(LDR));
int LEDleft = 11; // Left LED if (analogRead(LDR) <= 15) { // if input value from LDR <= 15
int LDR = A0; // LDR sensor digitalWrite(LEDleft,HIGH); // set left LED to HIGH (on)
digitalWrite(LEDright,LOW);// set right LED to LOW (off)
void setup() { delay(1000);
pinMode(LDR,INPUT); // declare LDR as input digitalWrite(LEDleft,LOW); // set left LED to LOW (off)
pinMode(LEDright,OUTPUT); // declare LEDright as output digitalWrite(LEDright,HIGH); // set right LED to HIGH (on)
pinMode(LEDleft,OUTPUT);// declare LEDleft as output delay(1000);
Serial.begin(9600); } else {
} digitalWrite(LEDright,LOW);
}
}
PIR-Passive Infrared Sensor
Motion Sensor
A passive infrared sensor (PIR
sensor) is an electronic sensor that
measures infrared (IR) light
radiating from objects in its field of
view. They are most often used in
PIR-based motion detectors.
You can read more here.
Based on materials from NCWIT.org/etextiles
House Sample Code void loop(){
sensorValue = digitalRead(input); // read input value
if (sensorValue == HIGH) { // check if the input is high
beep(500); //sound function
//Variables
if (currentState == LOW) {
int input = 2; // input pin for PIR sensor
Serial.println("Motion detected!");
int currentState = LOW; //assuming no motion detected
currentState = HIGH;
int sensorValue = 0; //reading PIR value (status)
}
int buzzer = 10; //PIN for Buzzer
}
else{ // If sensor input is low (no motion),
void setup() {
if (currentState == HIGH){ // and there was motion before,
pinMode(input, INPUT); // declare sensor as input
Serial.println("No motion detected!"); // print "No motion."
pinMode(buzzer, OUTPUT); // declare buzzer as output
currentState = LOW; // Set currentStatus to LOW (no motion).
Serial.begin(9200);
}
}
//give PIR sensor 30-60 seconds to stabilize.
}
Serial.print("Stabilizing sensor ");
for(int i = 0; i < 40; i++){
void beep(int delayms){ //Beep (sound) function
Serial.print(".");
tone(buzzer, 3000); // try different frequency!
delay(1000);
delay(delayms);
}
noTone(buzzer); // Stops any tone for pin 10
Serial.println("\ndone..."); // '\n' just to start a new line
delay(delayms);
}
}
Servo Motor SG90: DATA SHEET
The Ultrasonic Sensor

This sensor has 4 pins but we’ll use it as a 3 pin sensor. Ultrasonic Distance Sensor
● The echo pin - listens for the returned pulse
● The trig pin - gets triggered to start the detection
● 5v
● ground

5v
Trig Ground

Echo
Boat with Flag Sample Code
#include <Servo.h> void loop() {
Servo myservo; // variables for duration of the ping, and result in inches and cm
int pos = 0; long duration, inches, cm;

int dif = 0;
// The PING))) is triggered by a HIGH pulse of 2 or more msec.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
// pin number for the sensor's output:
pinMode(pingPin, OUTPUT);
const int pingPin = 7;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
void setup() {
digitalWrite(pingPin, HIGH);
myservo.attach(9);
delayMicroseconds(5);
Serial.begin(9600); // initialize serial communication:
digitalWrite(pingPin, LOW);
}
long microsecondsToInches(long microseconds) {
// The same pin is used to read the signal from the PING))): a HIGH pulse
return microseconds / 74 / 2; // whose duration is the time (in msecs) from the sending of the ping
} // to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
long microsecondsToCentimeters(long microseconds) { duration = pulseIn(pingPin, HIGH);
return microseconds / 29 / 2;
}
Boat with Flag Sample Code - continuation

inches = microsecondsToInches(duration);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
int val = map(cm,0,50,0,180);
Serial.print(cm);
val = val % 180;
Serial.print("cm");
if(abs(dif - cm) > 3){
Serial.println();
Serial.print("Rotating ");
delay(500);
Serial.print(val);
dif = cm;
Serial.println(" degrees");
}
for(pos = 0; pos <= val; pos++){
myservo.write(pos);
delay(15);
}
for(pos = val;pos >= 0;pos--){
myservo.write(pos);
delay(15);
}
}
MassBay Community College
ET 111 - iCREAT I

Project

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne Steiger-Escobar, Marina Bograd, Gauri Agrawal
This material is based upon work supported by the National Science Foundation under grant no. DUE-1501451
Any questions?
You can find me at:
[email protected]
[email protected]
[email protected]

Marina Bograd
Susanne Steiger-Escobar
Giuseppe Sena (Tony)
Credits

2016-2018 -- Shamsi Moussavi, Giuseppe Sena, Susanne


Steiger-Escobar, Marina Bograd
This material is based upon work supported by the
National Science Foundation under grant no. DUE-1501451

You might also like