Week 3
Week 3
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
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.
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);
• 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.
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.
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.