Professional Documents
Culture Documents
Training #1
Rawat Siripokarpirom
KMUTNB
E-mail: rawat.s @eng.kmutnb.ac.th
12 / 01 / 2018
Outline
3
Objectives
4
IoT Engineering Education
https://www.mbed.com/en/
9
STM32 Nucleo Boards (Mbed-enabled)
http://www.st.com/en/evaluation-tools/stm32-mcu-nucleo.html 11
Nucleo F401RE Board
12
Nucleo F401RE Board: Connectors
https://www.mbed.com/en/platform/hardware/boards/st/nucleo_f401re/ 13
Nucleo F401RE Board: Connector (Top-Left Side)
14
Nucleo F401RE Board: Connector (Top-Right Side)
15
Nucleo L053R8 (with STM32L053R8T6 MCU)
16
Nucleo L053R8 Board: Connectors
https://mbed.org/media/platforms/ST-Nucleo-L053R8-Arduino.png 17
Nucleo L432KC Board: Connectors
18
ARM Mbed OS Simulator
http://ec2-52-211-146-247.eu-west-1.compute.amazonaws.com:7829/
19
Two-LED Blinking
20
Two-LED Blinking : Demo Code (ARM Mbed Style)
DigitalOut redLed(p5);
DigitalOut blueLed(p6);
IoT Engineering Education
int main() {
redLed = 0; // Red LED off
blueLed = 1; // Blue LED on
wait_ms(100);
printf( "Toggle two LEDs\n" );
while (1) {
redLed = !redLed;
blueLed = !blueLed;
wait_ms(100);
}
}
21
Two-LED Blinking : Demo Code (Arduino Style)
#define RED_LED_PIN D5
#define BLUE_LED_PIN D6
IoT Engineering Education
void setup() {
pinMode( RED_LED_PIN, OUTPUT );
pinMode( BLUE_LED_PIN, OUTPUT );
digitalWrite( RED_LED_PIN, 0 ); // Red LED off
digitalWrite( BLUE_LED_PIN, 1 ); // Blue LED on
delay( 100 );
Serial.begin( 115200 );
Serial.print( "Toggle two LEDs\n" );
}
void loop() {
digitalWrite( RED_LED_PIN, !digitalRead( RED_LED_PIN ) );
digitalWrite( BLUE_LED_PIN, !digitalRead( BLUE_LED_PIN ) );
delay( 100 ); // delay in msec
}
22
Two-LED Blinking : Demo Code (Arduino C++ Style)
DigitalOut redLed( D5 );
DigitalOut blueLed( D6 );
void setup() {
redLed = 0; // turn off the Red LED
blueLed = 1; // turn on the Blue LED
wait_ms( 100 );
Serial.begin( 115200 );
Serial.printf( "Toggle two LEDs\n" );
}
void loop() {
redLed = !redLed; // toggle the Red LED pin
blueLed = !blueLed; // toggle the Blue LED pin
wait_ms( 100 ); // delay in msec
}
23
Write Your Own C++ Class (DigitalOut.h) for Arduino
private:
int _pin, _value;
};
24
ARM Mbed Signup & Login
https://os.mbed.com/account/login/?next=/platforms/
25
IoT Engineering Education
26
Platform: “NUCLEO-L053R8”
27
IoT Engineering Education
28
29
30
31
32
33
34
35
IoT Engineering Education
36
37
38
39
40
IoT Engineering Education
41
42
43
44
45
46
47
48
49
Digital Input and Output Programming
50
Digital Input and Output Programming
• https://os.mbed.com/docs/latest/reference/digitalin.html
• DigitalOut: a digital output pin Arduino’s digitalWrite()
• https://os.mbed.com/docs/latest/reference/digitalout.html
• DigitalInOut: a digital inout pin
• https://os.mbed.com/docs/latest/reference/digitalinout.html
51
DigitalIn Reference
https://os.mbed.com/docs/latest/reference/digitalin.html
PullUp, PullDown,
PullNone, OpenDrain
52
DigitalOut Reference
https://os.mbed.com/docs/latest/reference/digitalout.html
53
DigitalIn/Out Example: Examples of logical operations
DigitalIn a(D0);
DigitalIn b(D1);
IoT Engineering Education
DigitalOut z_not(LED1);
DigitalOut z_and(LED2);
DigitalOut z_or(LED3);
DigitalOut z_xor(LED4);
int main() {
while(1) {
z_not = !a; // NOT
z_and = a && b; // AND
z_or = a || b; // OR
z_xor = a ^ b; // XOR
}
} https://os.mbed.com/docs/latest/reference/digitalin.html
54
Digital Input and Output Programming
• https://os.mbed.com/docs/latest/reference/busin.html
• BusOut: a digital multiple-bit output pins
• https://os.mbed.com/docs/latest/reference/busout.html
• BusInout: a digital multiple-bit inout pins
• https://os.mbed.com/docs/latest/reference/businout.html
55
Digital Input and Output Programming
• https://os.mbed.com/docs/latest/reference/portin.html
• PortOut: a digital multiple-bit output pins
• https://os.mbed.com/docs/latest/reference/portout.html
• PortInout: a digital multiple-bit inout pins
• https://os.mbed.com/docs/latest/reference/portinout.html
The PortIn / PortOut / PortInOut classes are much less flexible but faster than the BusIn,
BusOut/ BusInOut classes; they are constrained by the port and bit layout of the
underlying GPIO ports.
56
Analog Input and Analog Output Programming
57
More Mbed Drivers
• TimeOut, Timer
• Ticker, LowPowerTicker
• Communications:
• Serial, RawSerial
• SPI, SPISlave
• I2C, I2CSlave
58
Mbed Coding Example 1 (LED Blink)
int main() {
int state = 0;
while(1) { // endless loop
led = state; // update LED output
state ^= 1; // toggle state
wait_ms( DELAY_MS ); // delay in msec
}
}
59
Mbed Coding Example 2 (LED & Push Button)
int main() {
button.mode( PullUp ); // enable internal pullup on button pin
while (1) {
led = !button;
}
}
Note that:
1) The button is active-low; If the button is pressed, the input logic is 0, otherwise 1.
2) If the button is held down (pressed), the LED is ON.
60
IoT Engineering Education
Capacitor
LED
Push
Button
61
void irq_callback() {
button.disable_irq(); // disable the interrupt on the Button pin
changed = true;
}
62
Mbed Coding Example 3 (External Interrupt) - Part 2
int main() {
button.fall( &irq_callback ); // detect falling-edge
IoT Engineering Education
int main() {
led.period_ms( 2.0 ); // period = 2 msec, freq = 500 Hz
led = 0.0f; // value between 0.0 to 1.0 (100% duty cycle)
while(1) {
if ( value == 255 && pm == 1 ) { pm = -1; } // change to decrement
else if ( value == 0 && pm == -1 ) { pm = 1; } // change to increment
led = (value/255.0f); // update the PWM output value
value += pm;
wait_ms( 5 );
}
}
64
Mbed Coding Example 5 (RGB LED with BusOut)
int main() {
uint8_t value = 1;
while(1) {
rgb = ~value; // invert the logic (for active-low LEDs) and write to output
// Rotate shift-left
value = ((value << 1) | (value >> 2)) & 0b111;
wait_ms(500);
}
}
65
IoT Engineering Education
anode)
RGB LED
(common-
66
int main() {
led1 = 0;
led2 = 0;
ticker1.attach( &led1_toggle, 0.1 ); // callback every 0.1 seconds
ticker2.attach( &led2_toggle, 0.5 ); // callback every 0.5 seconds
while(1) {
wait_ms(10);
}
}
67
Mbed Coding Example 7 (Rotary Encoder) - Part 1
68
Mbed Coding Example 7 (Rotary Encoder) - Part 2
void encode_inputs() {
qa_pin.disable_irq();
IoT Engineering Education
int a = qa_pin.read();
int b = qb_pin.read();
if ( (a != last_a) || (b != last_b) ) {
if ( a == b ) { encoder_cnt++; }
else { encoder_cnt--; }
encoder_update = true;
last_a = a;
last_b = b;
}
qa_pin.enable_irq();
}
int main() {
// qa_pin.mode( PullUp );
IoT Engineering Education
// qb_pin.mode( PullUp );
qa_pin.fall( &encode_inputs );
qa_pin.rise( &encode_inputs );
qa_pin.enable_irq();
pc.printf( "Rotary Encoder Reading Demo\r\n" );
while(1) {
if ( encoder_update ) {
encoder_update = false;
pc.printf( "Encoder Counter: %d\r\n", encoder_cnt );
}
}
}
70
IoT Engineering Education
71
72
int value = 0; // used to store the PWM duty cycle between 0..100
int pm = 1; // count direction: 1 = Up or -1 = Down
int main() {
while(1) {
aout = value/255.0f;
if ( value == 255 && pm == 1 ) {
pm = -1; // decrement
} else if ( value == 0 && pm == -1 ) {
pm = 1; // increment
}
value += pm;
wait_ms(10); // delay for 10 ms
}
}
73
Mbed Coding Example 9 (AnalogIn / AnalogOut)
int main() {
uint16_t value;
while(1) {
value = ain.read_u16(); // value
pc.printf( "%u\r\n", value );
aout.write_u16( value );
wait_ms(100); // delay for 100 ms
}
}
74
Mbed Coding Example 10 (Ultrasonic Sensor) – Part 1
Timer timer;
75
Mbed Coding Example 10 (Ultrasonic Sensor) – Part 2
float measure() {
// generate a TRIGGER pulse (20 usec width)
IoT Engineering Education
trig = 1;
wait_us( 20 );
trig = 0;
while ( echo == 0 ) { wait_us(10); } // wait for rising edge
timer.reset(); // reset timer
while ( echo == 1 ) { wait_us(10); } // wait for falling edge
uint32_t duration_us = timer.read_us();
float distance = (sound_speed * duration_us)/(1000000*2);
return distance;
}
int main() {
timer.start(); // start timer
IoT Engineering Education
while(1) {
float distance = measure();
if (distance > 5000)
pc.printf( "Distance: Out of range !!!\r\n" );
else
pc.printf( "Distance: %.1f cm\r\n", distance );
wait_ms(500);
}
}
77
IoT Engineering Education
78
79
void irq_callback() {
btn.disable_irq();
sleep_state = !sleep_state;
}
// Code continues on the next page.
80
Mbed Coding Example 11 (Sleep & Wakeup) – Part 2
while(1) {
if ( sleep_state ) { // enter sleep mode
led1 = led2 = 0; // turn off LED1 and LED2
btn.enable_irq();
sleep();
wait_ms(10);
btn.enable_irq();
pc.printf( "Woken up from sleep\r\n" );
} else {
led1 = 1; // turn on LED1
}
led2 = !led2; // toggle LED2
wait_ms( 100 );
}
}
81
Mbed Coding Example 12 (BH1750) – Part 1
82
Mbed Coding Example 12 (BH1750) – Part 2
bool bh1750_init() {
int ack;
IoT Engineering Education
uint8_t buf[2];
buf[0] = BH1750_CONT_HIGH_RES_MODE;
ack = i2c.write( addr, (const char *)buf, 1 );
wait_ms(10);
if ( ack != 0 ) { // no ACK
return false;
}
return true;
}
84
Mbed Coding Example 12 (BH1750) – Part 4
float bh1750_read() {
int ack;
IoT Engineering Education
uint8_t buf[2];
85
Mbed Coding Example 12 (BH1750) – Part 5
int main() {
i2c.frequency( 400000 );
IoT Engineering Education
i2c_scan();
bh1750_init(); // initialize the sensor
pc.printf( "Reading BH1750 light sensor...\r\n" );
while(1) {
float lux = bh1750_read();
if ( lux > 0 ) {
pc.printf( "Read: %d Lux\r\n", (int)lux );
} else {
pc.printf( "Sensor reading error !!!\r\n" );
}
wait(1.0); // 1 sec
}
}
86
IoT Engineering Education
87
88
89
90
91
To learn more
Please follow us
https://www.facebook.com/iot.kmutnb/
92