Tutorial: arduino-stm32 Part2
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.
Used for communication between the board and a computer or other devices.
baudrate : the data rate in bits per second (9600, ...)
Create a new program named as ‘TU_arduino_UART’.
Write the following source code.
Example 1: Print the Input Key
Example 2: Printing every 3 seconds
Click on Upload button.
Installing Serial Monitor
Download 'TeraTerm' Click Releases and download *.exe file
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
Object Proximity Detection
Print only when it detects the presence of an object.
Use your hand as the object and put it near the sensor
It should print " Warning! An object is too close"
Do not print anything when the object is not near.
The experiment kit has an IR proximal sensor that can detect the presence of an object.
It is connected at
PinName D4
DigitalIn
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.
Example: time measure
Create a new program named as ‘TU_arduino_Timer’.
Write the following source code.
Click on Upload button.
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 ‘millis()’ command.
Exercise
Time delay polling
Create a time delay function named as
delaycnt(uint cnt)
It receives the number of counts that it should wait.
cnt should be from 0~ 255
Print how many micro-seconds it took during the waiting
It should print " Counting 000 took 00 us"
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.
Example: ultrasonic sensor distance
Create a new program named as ‘TU_mbed_PWM1’.
Write the following source code.
Click on Upload button.
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 Upload button.
Press the reset button(black) and verify the operation. The distance between ultrasonic sensor and obstacle will be shown in Tera Term.
Exercise1:
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_arduino_PWM2’.
Write the following source code.
Click on Upload button.
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
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
Direction pin is PC_8 (But, you should wire
PC8
toD9
and useD9
pin in arduino example.)DIR=0, CW
DIR=1, CCW
PWM period is 1msec
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.
Exercise: analog photodetector
Create a new program named as ‘TU_arduino_AnalogIn.
Write the following source code.
Click on Upload button.
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: photodetection
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: sound sensor
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.
Last updated