The introduction to Arduino labs at Malmö University. These slides have been handed down since the beginning of Arduino. They have more authors then i can remember and should by no means be considered mine.
2. SERIAL COMMUNICATION
Serial.xxxx
Serial is a class designed to use serial communication
Serial.begin(baud);
Placed in setup() to initiate serial communication at a certain baudrate (speed)
Typical transmission speed for many existing devices is 9600 bps (bits per second)
Serial.print();
Writes (sends) anything to the serial port
Serial.println();
Writes (sends) with CR (carriage return) and LF (line feed)
Serial.available();
Used to check if data has arrived over the serial port
Serial.read();
Reads the first byte that arrived to the port and erases it from the communication buffer
2
4. SERIAL COMMUNICATION
is used to have your Arduino and your
personal computer ‘talk’ to each other
(send data)
SERIAL
4
5. ASCII vs. RAW BYTES
ASCII stands for American Standard Code for
Information Interchange
Alphanumeric characters are represented by
numbers ranging from 0 to 127 and are translated
into a 7-bit binary code. ASCII allows for easy
transfer of text-only files between different kinds of
computers
ASCII can be extended to cover symbol tables for
other languages
5
6. ASCII vs. RAW BYTES
One could say that ASCII is kind of a markup
language for symbols
The symbol 'A' (which represents the capital letter
A) is represented by numeric code 65, the symbol
for number '1' by numeric code 49, etc.
The communication between devices is encoded
using this standard
This allows including special symbols like:
LineFeed (LF), Carriage Return, Tab(ulator) (T)...
6
8. ASCII vs. RAW BYTES
Arduino uses 8N1 serial communication, this means
that information is encoded in packages of 8-bits
(1 byte)
With 8-bits we can encode 255 different symbols
If we were just sending numbers, smaller than 255,
we wouldn't need to encode the data. We could just
send bytes
Most of the times, this is not the case, therefore we
need to use ASCII as a code
8
9. ASCII vs. RAW BYTES
Serial.print() or Serial.println() will, by
default, encode all the data as ASCII
Therefore, the command: Serial.print(1) will
send the code 49 over the serial line
Serial.println(1) will send, in this order, 49-
10-13
Terminal programs like Bray-Terminal for Windows,
or Gnome Serial for Linux can show the pure data
sent
9
10. ASCII vs. RAW BYTES
• Arduino's IDE Serial Monitor is ready to present
only ASCII data
• This means that the black window at the IDE's
bottom will display only the ASCII representation
of numbers
• Therefore, Serial.print(67, BYTE) will
show character 'C'
• Serial.print(1) will show character '1'
• BYTE is commonly used to send information to
programs like Processing and Puredata
10
11. ABOUT THE SPEED
Arduino is a small but powerful computer designed
to do just three tasks at the same time:
– send/receive serial data
– operate analog outputs
– compute your program
Arduino’s processor works at 16MHz, and makes
about 1.000.000 operations/second.
If you send data at that pace, there is no computer
in the world that can follow
11
12. ABOUT THE SPEED
It is necessary to wait a short amount of time
between messages when sending data from your
Arduino to your computer otherwise you might run
the risk of hanging up your computer.
You should use delay(50)if you want the Serial
Monitor to display what comes through the port. If
the delay is any shorter, it’s hard to read the values.
Personal computers are capable of doing many
things at the same time. Arduino is good at doing
only one particular task very quickly, over and over
again.
12
14. SENDING SERIAL BACK
• We can also send data from the computer
to Arduino
• The best is to make use of a simple
character or symbol-based communication
protocol
• It is of course possible to send full strings
between the computer and Arduino, but
that will require too much effort in parsing
the information
14
16. SOFTWARE TO USE
Connecting to Flash and other programs like
PureData, Processing, MAX/MSP, VVVV, Isadora,
etc is a story for another time, it involves serial
communication in both directions.
There are plenty of examples of installations that
make use of serial communication
Also, two Arduinos can communicate with each
other through serial communication, using the TX
and RX pins.
16
17. EXERCISE ►MAKING 2 ARDUINOS TALK
• In pairs, connect
your Arduinos
• Connect RX to TX
• Connect TX to RX
• GND to GND
• Create a protocol to
do the Physical
pixel between two
Arduinos
17
18. SMALL SOUNDS
PIEZO ELEMENT/BUZZER
a.k.a. CONTACT MICROPHONE
The piezo element is both a sensor and an actuator
As sensor:
Check the example ANALOG/KNOCK
As actuator:
Electronic music greeting cards
18
19. SMALL SOUNDS
Piezo speaker can be replaced by a stereo plug
which you connect to another sound output
*BEWARE* the sound output is VERY high when
connected to a stereo or headphones
=
19
20. HOW TO PLAY SOUNDS
Piezos consist of materials which attract / repel each
other (vibrate) when flows through them.
Quick vibrations on the piezo element produce sounds
We only need to connect the piezo directly to an output
and write the pin HIGH and LOW
We will use the method delayMicroseconds(time)to
create soundwaves
20
21. EXAMPLE ►PLAY “A”
int outputPin = 9; // plug the piezo to pin 9
void setup() {
pinMode(outputPin, OUTPUT); // declare the piezo pin as
an output
}
void loop() {
digitalWrite(outputPin, HIGH);
delayMicroseconds(1136); // the time here will
// determine the
tone
digitalWrite(outputPin, LOW);
delayMicroseconds(1136); // the time here will
// determine the tone
}
21
22. EXERCISE ►FM OSCILLATOR
Connect a potentiometer to an analog
input, the Piezo element to a ‘digital’ input
and make a sound generator which lets you
change the tone by turning the potentiometer
22
potentiometer
23. EXERCISE ►OSCILLATOR
int outputPin = 9; // plug the piezo to pin 9
int inputPin = 2; // the potentiometer at analog 2
void setup() {
pinMode(outputPin, OUTPUT); // declare the piezo pin as an
output
}
void loop() {
int val = analogRead(inputPin); // read the potentiometer into a
variable
digitalWrite(outputPin, HIGH);
delayMicroseconds(val); // the time here will determine the
tone
digitalWrite(outputPin, LOW);
delayMicroseconds(val); // the time here will determine the
tone
}23
25. HOMEWORK: SERIAL KEYBOARD
Make a program in Arduino that plays sounds when
receiving characters over the serial port
e.g. the character 'E' should play the tone 'E4'
The interaction with the program will happen
entirely through serial communication
HINT: the table on next slide shows the
delayMicroseconds value for the notes
Go wild on your serial synthesizer!
25
Encourage people to interrupt you so you can help them immediately. Explain they do you a favor.
It’s good to time the duration of the presentation.
Serial communication is in a library because you might not always use it. So when you don’t use it you don;t have to include it
Serial.begin calls the library and starts it. You have to specify a communication speed in baud.
Arduino uses asynchronous serial communication, you have to use the same speed on both the devices that communicate to eachother.
Print() prints a message
We use Println to print a message and jump to the next line afterwards.
Computers don’t need the line feed and carriage return to be able to read the message.
Serial.available is used to check when there is activity on the serial port.
You use it to only read serial data when it’s available so the constant reading on the serial port doesn’t slow down the rest of the code.
Explain that the concepts of carriage return and line feed come from the original typewriters which needed to move the assembly holding the sheet of paper back to the beginning of the sentence and line feed to jump down to the next line.
Serial communication is in a library because you might not always use it. So when you don’t use it you don;t have to include it
Serial.begin calls the library and starts it. You have to specify a communication speed in baud.
Arduino uses asynchronous serial communication, you have to use the same speed on both the devices that communicate to eachother.
Print() prints a message
We use Println to
Computers don’t need the line feed and carriage return to be able to read the message.
Serial.available is used to check when there is activity on the serial port.
You use it to only read serial data when it’s available so the constant reading on the serial port doesn’t slow down the rest of the code.
Different computers use different ways of communication.
Which becomes a problem if everyone does their own thing.
This is why they invented a standard for communication.
Relate it to a keyboard.
Historically, a byte was the number of bits (0’s or 1’s) used to encode a single character of text in a computer.
When you type 65 on your screen and send it to Arduino, you’re not actually sending ‘65’ your sending the ASCII code for ‘A’
What you see is not what you get.
CR = 10
LF = 13
Talk about example of the IPCam with the LCD screen and ascii problems.
8N1 serial communication, Arduino sends all the information in packages of 8 bits. 8 bits = 1 byte.
Communicating between two arduino’s: send just one character, like move the motor up: send a ‘u’, move the motor down, send a ‘d’
You can’t send a whole string ‘turn the motor up’
ASCII is needs to we can encode more than 255 characters.
The numbers on your keyboard represent actually a sequence of numbers, just like the letter keys.
Show examples of how it works
Write a program from scratch which:
Prints stuff on the same line,
Print stuff on a new line each time
Has a different baud rate for sending and receiving, which results in garbled data
Which has different delays to show how it affects reading speed
Don’t do the processing part, just send the character for turning it on or off through the serial monitor.
Find examples
Pair them up in two groups:
One group keeps the physical example which controls the LED on port 13,
The other group writes code to control the LED on the other Arduino
Sound are just vibrations in air
Piezo speakers have the ability to vibrate really fast, to generate sound you move it at high speed.
Piezo speaker contains two metal sheets which vibrate close to each other to create the sound.
You can use it as a knock sensor aswell. Look up ‘knock’ example on arduino website and add it as slide. Add it after the play “a” slide
You can also replace
Only do it if you have a microphone control slider on your headphones otherwise you blow your ears out.
You can make a volume control with a potentiometer and piezo. Connect the piezo to power and left pin of pot, connect middle pin of pot to a pin.
Analogy: pushing against an object, when you push slow and hard it’s difficult to move and object, but when you make it vibrate softly very fast it becomes easier to move.
In theory it can make a table, a solid piece of wood, into a speaker aswell. In theory any material can make sound.
This value is fixed. The delay time determines which tone you are generating.
An oscillator is anything that can change it’s own resistance. Or anything that can change it’s own values.
Write this code together:
int outputPin = 9; // plug the piezo to pin 9
int inputPin = 2; // the potentiometer at analog 2
void setup() {
pinMode(outputPin, OUTPUT); // declare the piezo pin as an output
}
void loop() {
int val = analogRead(inputPin); // read the potentiometer into a variable
digitalWrite(outputPin, HIGH);
delayMicroseconds(val); // the time here will determine the tone
digitalWrite(outputPin, LOW);
delayMicroseconds(val); // the time here will determine the tone
}
Modify the code to change it so that all the values are multiplied by two to make it play a different sound
Skip frequency and period
Frequency is the number of oscillations and seconds.
Period: Tony doesn’t know
Maybe someone in class knows who has had musical training and plays an instrument.