Tutorial: mbed - Part 3
U(S)ART (Universal Asynchronous/synchronous Receiver and Transmitter)
We are going to create a simple program that links MCU-PC via UART communication. MCU can receive and transmit 8-bit character data through UART communication.
NUCLEO-F401RE board offers UART2 channel with USB connector.
mbed class
Look up in mbed documentation for the fulll list of methods
Create a **** new program named as ‘TU_mbed_UART’.
Write the following source code on ‘main.cpp’
Example 1: Print the Input Key
Example 2: Printing every 3 seconds
Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.
Open ‘Tera Term’ . and make New Connection.
Choose ‘Serial’ tab -> Select ‘COMx: STMicroelectronics STLink’ port
COMx, x is the port number and it can be different for each connection.
Open Serial port(시리얼 포트) in Setup(설정) tab, check if the baud rate is set as 9600 [bps].
Press the reset button(black) and verify the operation. If you put any letter in Tera Term, MCU will receive it and transmit it to PC immediately, so you can see the pushed letters showed in Tera Term.
Exercise
The experiment kit has an IR proximal sensor that can detect the presence of an object.
It is connected at
PinName D4
DigitalIn
Print only when it detects the presence of object by placing your hand near the sensor.
" Warning! An object is too close"
Do not print anything when the object is not near.
Timer
We are going to create a simple program that measures the time to count 100 starting from 0. Print the result through UART communication.
mbed class
Look up in mbed documentation for the fulll list of methods
Create a **** new program named as ‘TU_mbed_Timer’.
Write the following source code on ‘main.cpp’
Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.
Open ‘Tera Term’ and make New Connection.
Push the reset button(black), and verify the time taken in counting 100. You can measure time taken in any other processes like toggling LED, multiplication or division, etc. If the process takes long time, you can also measure time in [ms] unit using ‘timer.read_ms()’ command.
PWM (Pulse Width Modulation) Ultra Sonic Sensor
We are going to create a simple program that measure distance by using ultrasonic sensor ‘HC-SR04’ and print out result through UART communication.
mbed class
Look up in mbed documentation for the fulll list of methods
Create a **** new program named as ‘TU_mbed_PWM1’.
Write the following source code on ‘main.cpp’
Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.
Open ‘Tera Term’ and make New Connection.
Ultrasonic sensor ‘HC-SR04’ get trigger signal as 10[us] pwm through trig pin which generate on D10 pin. Also, you should capture the echo signal on D7 pin and measure its pulse-width to calculate the distance.
Press the reset button(black) and verify the operation. The distance between ultrasonic sensor and obstacle will be shown in Tera Term.
Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.
Press the reset button(black) and verify the operation. The distance between ultrasonic sensor and obstacle will be shown in Tera Term.
Exercise_1:
Measure the distance (cm) and the pulse width (msec) and print both values.
What is the maximum and minimum distance it can measure
What is the accuracy of the distance you have measured with the ultrasonic sensor?
Show your experiment result and compare it with the exact distance measured by a ruler.
Exercise_2:
Generate a square pulse of 1~2Hz by using a function generator.
Measure the time period of the pulse in msec.
What is the accuracy when measuring the period? What can you do to improve the measurement accuracy?
PWM (Pulse Width Modulation) DC - Motor
We are going to create a simple program that run DC - Motor by giving pwm signal as input.
Create a **** new program named as ‘TU_mbed_PWM2’.
Write the following source code on ‘main.cpp’
Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.
Press the reset button(black) and verify the operation. If you press the user button, DC-Motor will turn on.
Exercise
Control Motor direction and speed with following configuration
Direction pin is PC_8:
PinName PC_8
DIR=1, CW
DIR=0, CCW
PWM period is 1msec
As button B1 is pressed, increase PWM duty ratio by interval of 25%
0 - 0.25(CW)- 0.5(CW)-0.75(CW)-1(CW)- 0 - 0.25(CCW)- 0.5(CCW) and so on
ADC
We are going to create a simple program that measures the output voltage of photo-resistor and print out the result through UART communication.
mbed class
Look up in mbed documentation for the fulll list of methods
Create a **** new program named as ‘TU_mbed_AnalogIn.
Write the following source code on ‘main.cpp’
Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.
Open ‘Tera Term’ and make New Connection.
Photo-resistor module outputs low voltage under a bright condition, and vice versa.
Push the reset button(black) and verify the operation. If you turn on the flashlight at the photo-resistor with your phone, the sensor output voltage will be decreased. When the output voltage is below 0.2V, which means high brightness is given, the LED(LD2) will be turned on.
Exercise_1
Change different values for measure threshold to change the sensitivity of detection.
Under the brightness near the thresholding voltage, the led may flicker on and off. How can you change your code to avoid this flicker?
Exercise_2
The experiment kit has a sound sensor (microphone)[SZH-EK033]. You can change the sensitivity of the sound sensor by turning the variable resistor.
It is connected as AnalogIn
PinName A5
For every second, print the value of the sound sensor
Check the max value the sensor can print.
Turn LED on/off by clapping your hand.
Bluetooth
We are going to create a simple program that links MCU-PC via Bluetooth communication. MCU can receive and transmit 8-bit character data through the Bluetooth communication.
The experiment kit has Bluetooth HC-05.
You can connect the MCU to PC via bluetooth or an APP.
Android App Terminal Multi (Google Play Download)
Bluetooth SPP Manager (Google Play Download)
Refer here for more information about bluetooth
For example code, refer here
Last updated