SM2705 Creative Media Studio III: Technology, Coding & Tangible Media
Introduction to Arduino &
Review on Coding
Kening Zhu (Ken)
Associate Professor
School of Creative Media
City University of Hong Kong
Email:
[email protected]Introduction to Microcontroller
• A microcontroller (also microcomputer, MCU or µC) is a
small computer on a single integrated circuit consisting
internally of a relatively simple CPU, clock, timers, I/O
ports, and memory.
Arduino
What exactly is Arduino Board?
• Arduino is an open-source electronics prototyping
platform. It is intended for artists, designers, hobbyists,
and anyone interested in creating interactive objects or
environments.
The one we are using
is Arduino UNO, which is
made with Atmel
ATmega328.
Connection of Arduino Board
Power & Data
Power
Connecting Arduino Board to PC
• Connect the board to computer via USB.
Power
light
Connecting Arduino Board to PC
• The board is connected through the serial port (COM port)
• Mark down the port number in device manager (Here COM3)
Arduino Software Environment
• Download from Arduino Website
(http://arduino.cc/en/Main/Software )
• Install on your computer
Arduino Software Environment
• Here you can program and upload to the board
But you need to set up
the connection first
Programming Area
Set up connection in software
Set up connection in software
• This is in Windows
OS, how about in Mac?
Set up Arduino in Mac OS X
• Similarly connect your board through USB first
Set up Arduino in Mac OS X (Cont)
More tutorials and examples in..
• http://playground.arduino.cc/uploads/Main/arduino_noteb
ook_v1-1.pdf
• http://arduino.cc/en/Guide/HomePage#
• http://arduino.cc/en/Tutorial/HomePage
Now we can start programming!
• How does coding look like in Arduino?
Structure of an Arduino sketch
Both setup and loop are needed for the code to work.
You can define other functions by yourself.
Let’s try “Hello World” in Arduino
• Similarly connect your board through USB first
To see the text output
What the code means
• We can show texts/values in the serial monitor in Arduino software.
• Usually for debugging
• Similar to println() and print() in Processing
• Serial.begin(9600);
• Starting the serial communication channel.
• Usually only need to run once in the setup function.
• Serial communication could be set up for usb connectin and bluetooth connection.
• Serial.println(“Hello World”);
• Send a line to the serial channel.
• The sent message can be caught and displayed in the serial monitor.
• Serial.print(“text”);
• Send a string to the serial channel without the line breaker.
• You can use this channel to communicate with and controll other devices.
• Such as another Arduino, PC, mobile phone, etc.
• We will talk about in another lecture later in the semester.
What will this program output in the serial
monitor?
What will this program output in the serial
monitor?
Variables (Similar to Processing)
• A variable is a way of naming and storing a value (e.g.
numbers, texts, etc.) for later use in the program.
• A variable needs to be declared with a type and a unique
name, and optionally assigned to the value.
• There are different types of variables
Nested Loop
• Loop inside loop
• If Loop A is inside Loop B, then A will be repeated for several times
LED + Arduino
• Blinking
Circuit
Pin Number
• Here we give a name, a type, and a value to a variable
• Type: int, means it is an integer
• Name: ledPin, the name is unique in the program, just like your ID
• Value: 13, you can assign any value
• Here we assign the Pin number on the board to this variable
• We have pin 0 to 13 as digital pins
Using new functions
pinMode(13, OUTPUT);
- initialize pin 13 as an output pin, pin mode can be INPUT or OUTPUT.
- You can also put the variable name here pinMode(ledPin, OUTPUT);
digitalWrite(13, HIGH);
- supplies high voltage (5 volts) to pin 13, voltage value can be HIGH or LOW.
digitalWrite(13, LOW);
- supplies low voltage (0 volts) to pin 13, voltage value can be HIGH or LOW.
delay(1000);
- tell the Arduino to do nothing for 1000 milliseconds. (1000 ms = 1s)
Let’s modify a bit the code and upload
Change the pin number
Change the blinking speed
Task 1: More LEDs with Arduino
• Use the Arduino to control three LEDs, making them flash
from left to right.
Now, let’s make some input function
• pinMode(pinNumber, OUTPUT);
• This is output mode.
• We can also identify a pin hole to accept input from a button.
• pinMode(pinNumber, INPUT);
• digitalWrite(pinNumber, HIGH);
• This is writing a output
• We can also read an input from a pin
• digitalRead(pinNumber);
The circuit with button (Press)
Pull-UP and Pull-DOWN resistors.
• A pull-up resistor is a resistor connected between a
signal conductor and a positive power supply voltage
to ensure that the signal will be a valid logic level.
5V (+)
• A pull-up resistor pulls the voltage of the signal it is
connected to towards its voltage source level. When
the other components associated with the signal are
inactive, the voltage supplied by the pull up prevails
and brings the signal up to a logical high level.
• When another component on the line goes active, it
overrides the pull-up resistor. When the switch is
closed (on) the PIN has a direct connection to GND,
which takes it to the low state.
• The pull-up resistor ensures that the wire is at a
defined logic level even if no active devices are
connected to it.
GND (-)
• A pull-down resistor works in the same way but is
connected to ground. It holds the logic signal at a low
logic level when no other active device is connected.
Push-down resistor
5V (+)
GND (-)
The code for button
Now we know
When we push the
button, the input of
pin 3 is HIGH,
otherwise it is
LOW
New codes
digitalRead(switchPin);
- read digital value from a pin.
Comparison operators (==, !=, >, <, >=, <=)
x > 120
value == HIGH
- compare two values and give a boolean (true / false) value
IF structure
if (condition)
{
// body
}
else
{
//else
}
- execute the body part if the condition is true.
Task 2: Use the button to switch on/off
theLED
BonusTask: Press the button to display
different patterns of 4 LEDs
• Design a visualization of animated patterns: left-to-right,
right-to-left, bounce, etc. Use the button to cycle through
all the visualizations.