Week 5
Week 5
Topic
Lecture 24: Serial Port Terminal Application
(Coolterm)
About CoolTerm
Configuration
Demonstration
Introduction
• Sensor data depends on the connection as well as on noise so we need to check the value of
sensor.
• The value must be printed on the screen.
• To print the value on the PC screen we need a communication channel.
• A serial communication link between the PC and microcontroller is used to transfer data from
microcontroller to PC.
• Software tools such as CoolTerm, TeraTerm, etc. are used to establish the serial communication
and to print the data on the PC screen.
3
HyperTerminal
• Software Tools that enables serial communication between the desktop/laptop and the
microcontroller are termed as HyperTerminal Tool.
• Examples: CoolTerm, TeraTerm, etc.
• For our experiments, it is required to ensure that there is some HyperTerminal installed on the
computer.
• Will enable serial data communication with the microcontroller.
• HyperTerminal connections in the present context shall be made using USB port.
4
CoolTerm
5
Settings on CoolTerm
• For proper data communication, proper settings have to be made on the HyperTerminal
software.
• We shall explain the steps for CoolTerm.
• The default serial configuration in CoolTerm is 9600 bauds, 8-bit data.
• If you have not set any bauds rate in the program, then no need to change on this.
Else it can be set to match with the program.
6
Connect
7
Configuration
8
Example
9
Output
10
11
Course Name: Embedded System Design with ARM
Faculty Name: Dr. Kamalika Datta
Department : Computer Science and Engineering
Topic
Lecture 25: Experiment with Temperature Sensor
Interfacing LM35 to STM32 board
Demonstration
Temperature Sensing
3
LM35 Temperature Sensor
• Precision integrated-circuit temperature device with an output voltage
linearly proportional to the temperature in degrees Celsius (or
Fahrenheit).
• Linear characteristic: +10 mV/°C change in output.
• Rated for full −55°C to 150°C range.
• How to use the LM35?
• Connect power supply (say, +5V and GND) to pins 1 and 3.
• Measure the analog voltage output on pin2.
• We can connect pin 2 to one of the analog input pins of the microcontroller.
4
Reading Analog Value from LM35
• Connect the two extreme terminals to #include “mbed.h”
AnalogIn sensor(A1);
power (say, 5V) and GND. …
• The middle terminal will produce a voltage float p;
proportional to the temperature. p = sensor.read();
…
pc.printf (“\n Value read: %f”, p);
…
5
Calibration
• LM35 output voltage increases linearly with increase in temperature.
• 10 mV increase for every °C rise in temperature.
• For k°C, the output voltage will be 10k mV.
• If maximum temperature is 50°C, maximum output voltage will be 0.5 V (10x50 = 500).
6
• Alternate way of computing:
• Suppose the room temperature is 20°C
• Using Coolterm, we run the program and suppose we find the value printed as 0.06
• For any unknown temperature, if the value printed is P, the temperature in °C will be
20P / 0.06
7
An Experiment using LM35
• Design a Temperature Sensing Unit using STM32 Nucleo-F401RE kit and a LM35
temperature sensor to sense the environmental temperature, and display it in
Centigrade scale on a LCD display unit.
• The analog output voltage from LM35 is connected to pin A1 of the Arduino connector of the
STM32 board.
8
Connection Diagram
9
int main(){
Mbed Program Code int val=297; // after calibration
char buf[20];
float p;
#include "mbed.h" lcd.setLine(0, "Temp in Degree C");
#include "string.h" while(1) {
#include "TextLCDScroll.h” p = sensor.read();
sprintf(buf, "%4.2f", p*val);
TextLCDScroll lcd (D13, D12, D11, D10, D9, lcd.setLine(1, buf);
D8, TextLCD::LCD16x2); wait(1);
Serial pc (USBTX,USBRX); }
AnalogIn sensor(A1); }
DigitalOut led(D2);
10
11
Course Name: Embedded System Design with ARM
Faculty Name: Dr. Kamalika Datta
Department : Computer Science and Engineering
Topic
Lecture 26: Experiment with LDR Light Sensor (Part 1)
LDR interface circuit
Demonstration
Light Dependent Resistor (LDR)
• A LDR is a variable resistor (a passive
component) whose resistance value changes
depending upon the amount of light falling on
it.
• More the amount of light, less will be the
resistance and vice versa.
• The variation of resistance with light intensity is
non-linear.
• A simple resistance divider can generate an
analog voltage that depends on RLDR.
3
LDR Interfacing
• First test the LDR by measuring its resistance.
• Consider for example, RLDR = 150 KΩ (no light), and RLDR = 10 KΩ (light)
V = 5V
I = V /(RLDR + R1)
Vout = I * R1
LDR RLDR
= V * R1 /(RLDR + R1)
Vout = 5 * R1 /(RLDR + R2)
R1
4
How to choose R1?
• Within the range of light intensity for some application,
the variation in output voltage should be appreciable. R1 Vout Vout
(light) (no light)
• Consider for example, RLDR = 150 KΩ (no light), and
1 KΩ 0.45 V 0.03 V
RLDR = 10 KΩ (light)
5 KΩ 1.67 V 0.16 V
Vout = 5 * R1 /(RLDR + R1)
10 KΩ 2.50 V 0.31 V
20 KΩ 3.33 V 0.56 V
50 KΩ 4.17 V 1.25 V
5
Nonlinear Variation of Resistance with Light
6
The Experiment
• Create a “Room Light Controller” using LDR. Sense the ambient light and adjust the intensity of
the room light accordingly.
• For demonstration, we use a LDR circuit connected to the analog input pin A1 of the Arduino interface.
• Instead of a bulb (in a room), we use a LED that is connected to the PWM digital output pin D3.
• The duty cycle of the PWM signal is varied depending on the level of light.
• In the experiment, we define three levels with corresponding duty cycles 1.0 (fully on),
0.8 (somewhat dim), and 0.0 (off).
• The LED glows when D3 is at 0.
7
• How are the threshold values determined?
• Have been fixed through experimentation.
• The analog input voltage read from LDR output (0 to 1 range) is multiplied by 5000.
• The three threshold ranges are fixed at 200, 500 and 850.
• Less than 200 :: PWM duty cycle = 0.0, and so on.
8
Connection
Diagram
9
Mbed C Code for STM32
#include "mbed.h" if (in2>850) {
PwmOut LED(D3); LED=1.0;
AnalogIn myIn(A1); }
int main() else if (in2>500 && in2<850) {
{ LED=0.8;
float in1; }
int in2; else if (in2<200) {
while (1) LED=0.0;
{ }
in1 = myIn.read(); }
in2 = in1*5000;
10
11
Course Name: Embedded System Design with ARM
Faculty Name: Dr. Kamalika Datta
Department : Computer Science and Engineering
Topic
Lecture 25: Experiment with LDR Light Sensor (Part 2)
Experiment of object counting using LDR
light sensor
Demonstration
The Experiment
• Design a counter (using LDR and LED’s), which will display the number of persons present in a
room on a 7-segment LED display
• [Hint: Use two LDRs to detect direction of motion i.e., whether a person
is entering or leaving]
3
The Arrangement
LDR
LDR1 LDR
LDR1
4
Basic Concept
a) Initialize counter to 0.
b) If the light intensity read by LDR from entry point decreases, increment the
counter.
c) If the light intensity read by LDR from exit point decreases, decrement the
counter.
5
Connection Diagram
6
void Display(int disp){
The Mbed C Code switch(disp)
{
case 0: A=0;B=0;C=0;D=0;E=0;F=0;G=1; break;
#include "mbed.h" case 1: A=1;B=0;C=0;D=1;E=1;F=1;G=1; break;
#include "string.h" case 2: A=0;B=0;C=1;D=0;E=0;F=1;G=0; break;
DigitalOut A(D2); case 3: A=0;B=0;C=0;D=0;E=1;F=1;G=0; break;
DigitalOut B(D3); case 4: A=1;B=0;C=0;D=1;E=1;F=0;G=0; break;
DigitalOut C(D4); case 5: A=0;B=1;C=0;D=0;E=1;F=0;G=0; break;
DigitalOut D(D5); case 6: A=0;B=1;C=0;D=0;E=0;F=0;G=0; break;
case 7: A=0;B=0;C=0;D=1;E=1;F=1;G=1; break;
DigitalOut E(D6);
case 8: A=0;B=0;C=0;D=0;E=0;F=0;G=0; break;
DigitalOut F(D7); case 9: A=0;B=0;C=0;D=0;E=1;F=0;G=0; break;
DigitalOut G(D8); }
AnalogIn Entry(A1); }
AnalogIn Exit(A2);
7
int main(){ else if(ex<200) {
float en, ex; per=per-1; Display(per);
int per=0; wait(1);
while(1) { }
en = Entry.read(); else if (per<=0 || per>9) {
ex = Exit.read(); per=0; Display(per);
en=en*5000; wait(1);
ex=ex*5000; }
if (en<200) else
{ {
per=per+1; Display(per);
Display(per); wait(1);
wait(1); }
} }
}
8
9
Course Name: Embedded System Design with ARM
Faculty Name: Dr. Kamalika Datta
Department : Computer Science and Engineering
Topic
Lecture 28: Experiment with Speaker
Speaker interface circuit
Demonstration
How does a speaker work?
3
How to Interface a Speaker?
• Any waveform in the audio frequency range from an output port can drive the speaker.
• May require an amplifier circuit to generate adequate power for the electromagnet.
4
• Small speakers (e.g. 8 ohms or less) can also be driven directly from the port lines of a
microcontroller.
• Of course, sound output will be low.
From digital
port
Ground
5
Experiment 1
6
The PwmOut API :: Relevant Member Functions
7
Connection Diagram – STM32
D3/PWM
STM32F401RE
GND
8
Mbed program for Speaker (beep)
#include "mbed.h"
DigitalOut speaker(D3);
int main() {
while(1) { This program does not use PWM. It
speaker = 0; simply outputs 0 and 1 alternately,
with time period 4 msec, i.e.
wait (0.002); // Wait 2 msec
frequency of 250 Hz.
speaker = 1;
wait (0.002); // Wait 2 msec
}
}
9
Experiment 2
10
Mbed program for Speaker (Ambulance Sound)
#include "mbed.h"
PwmOut mypwm(D3);
int main() {
mypwm.period_us(3000);
mypwm.pulsewidth_us(1550);
while(1) {
mypwm.period_us(3000);
mypwm.pulsewidth_us(1550);
wait(0.5); // Play tone-1 for 0.5 sec
mypwm.period_us(2200);
mypwm.pulsewidth_us(100);
wait(0.5); // Play tone-2 for 0.5 sec
}
}
11
Experiment 3
12
#include "mbed.h"
PwmOut speaker(D3);
int main() { Mbed
float freq, period;
int i;
program for
speaker.write (0.5); // Set duty cycle to 0.5 Speaker
while (1) {
freq = 300.0; // Start with 300 Hz
(Sa Re Ga Ma)
for (i=0; i<12; i++) {
period = 1.0 / freq;
speaker.period (period);
wait (0.5);
freq = freq * 1.0595; // Multiply by 12th root of 2
}
wait (1);}
}
13
Experiment 4
14
wait (0.5);
freq = 294.0;
#include "mbed.h"
period = 1.0 / freq;
PwmOut speaker(D3);
speaker.period (period); Mbed
wait (0.5);
int main() {
freq = 262.0; program for
float freq, period;
int i;
period = 1.0 / freq; Speaker
speaker.period (period);
speaker.write (0.5);
wait (0.5);
(Musical Tone)
// Set duty cycle to 0.5
freq = 349.0;
while (1) {
period = 1.0 / freq;
freq = 262.0;
speaker.period (period);
period = 1.0 / freq;
wait (0.5);
speaker.period (period);
freq = 330.0;
wait (0.5);
period = 1.0 / freq;
freq = 262.0;
speaker.period (period);
period = 1.0 / freq;
wait (0.5);
speaker.period (period);
wait (0.5);
}
}
15
16
Course Name: Embedded System Design with ARM
Faculty Name: Dr. Kamalika Datta
Department : Computer Science and Engineering
Topic
Lecture 29: Experiments with Microphone
Microphone interfacing
Demonstration
What is a Microphone?
3
Typical Microphone Interfacing Circuit
4
Microphone Module with Interface
5
Experiment 1
• Implement a clap switch, where a LED can be turned ON using the sound from a
clap.
• The microphone circuit is interfaced to the AnalogIn pin A1.
• The AnalogIn object converts the input voltage to floating-point numbers from 0.0 to 1.0.
• Through experimentation, we can determine the threshold value, which would depend on
the noise of the environment.
• The microphone module used, however, generates a 2-level digital output.
• A LED is connected to the DigitalOut pin D2, where the LED glows if the
D2 pin is at 0.
• The LED turns on for 3 seconds, and then turns off.
6
Connection Diagram – STM32
7
Mbed program for Microphone
#include "mbed.h"
AnalogIn analog_value(A1);
DigitalOut led(D2);
int main() {
float meas;
while(1) {
led = 1;
meas = analog_value.read();
if (meas>=0.7){
led = 0;
wait(3);
led = 1;
}
}
}
8
Experiment 2
9
Connection Diagram – STM32
10
Mbed program for Microphone (Clap Count)
void Display(int disp) {
#include "mbed.h"
switch(disp)
DigitalOut A(D2); {
DigitalOut B(D3); case 0: A=0;B=0;C=0;D=0;E=0;F=0;G=1; break;
DigitalOut C(D4); case 1: A=1;B=0;C=0;D=1;E=1;F=1;G=1; break;
case 2: A=0;B=0;C=1;D=0;E=0;F=1;G=0; break;
DigitalOut D(D5); case 3: A=0;B=0;C=0;D=0;E=1;F=1;G=0; break;
DigitalOut E(D6); case 4: A=1;B=0;C=0;D=1;E=1;F=0;G=0; break;
DigitalOut F(D7); case 5: A=0;B=1;C=0;D=0;E=1;F=0;G=0; break;
case 6: A=0;B=1;C=0;D=0;E=0;F=0;G=0; break;
DigitalOut G(D8);
case 7: A=0;B=0;C=0;D=1;E=1;F=1;G=1; break;
AnalogIn val(A1); case 8: A=0;B=0;C=0;D=0;E=0;F=0;G=0; break;
case 9: A=0;B=0;C=0;D=0;E=1;F=0;G=0; break;
}
}
11
int num=0;
int main()
{
float meas;
while(1) {
meas = val.read();
if (meas >= 0.7) {
num++;
wait(0.1);
}
if (num > 9) {
num=0;
}
Display(num);
}
}
12
13