Tutorial: How to use the Serial port on mbed

On this tutorial, we will show how to send messages using the serial communication. The mbed has 3 serial ports that can be configured. This can be used to communicate with other ICs or for debug purposes. Also, the board has a serial port that can be emulated via USB:

Connect the mbed to your PC. Open the compiler as in http://garagelab.com/profiles/blogs/tutorial-mbed-first-steps. Open that link and import that library to the program.

Inside the main.cpp file, paste the code:

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
PwmOut led(LED1);

float brightness = 0.0;

int main() {
pc.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down\n");

while(1) {
char c = pc.getc();
if((c == 'u') && (brightness < 0.5)) {
brightness += 0.01;
led = brightness;
}
if((c == 'd') && (brightness > 0.0)) {
brightness -= 0.01;
led = brightness;
}

}
}

Open a serial terminal program, like PuTTY:

When you press u the onboard LED will get brighter, if you press d, it will fade out.

That's it! Enjoy using your board with serial communication.

References:

http://mbed.org/handbook/mbed-NXP-LPC1768

http://mbed.org/handbook/Serial

http://mbed.org/handbook/SerialPC

Views: 8601

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

iftekhar Mobin is now a member of GarageLab (arduino, electronics, robotics, hacking)
yesterday
Profile IconLeicester Hibbert and Michael Young joined GarageLab (arduino, electronics, robotics, hacking)
Jan 9
Profile IconThorsten Schwarz and Dana Custer joined GarageLab (arduino, electronics, robotics, hacking)
Jan 2
Aqeel munir updated their profile
Dec 31, 2016
Todd Enger updated their profile
Dec 29, 2016
FRANK KWABENA is now a member of GarageLab (arduino, electronics, robotics, hacking)
Dec 28, 2016
Ayhan Chaplan commented on GarageLab's blog post How to use a Peltier with Arduino
"Hi! Will this setup work with a 3,8V / 2,5A Peltier element? Or do I have to reduce the resistance…"
Dec 26, 2016
Profile IconAyhan Chaplan, Erva and Donna Carter joined GarageLab (arduino, electronics, robotics, hacking)
Dec 26, 2016
Galy Huang posted a discussion

Ubuntu on Firefly-RK3399

Firefly-RK3399 with dual-core Cortex-A72 and quad-core Cortex-A53, ARM® Mali-T860 MP4 quad-core…See More
Dec 20, 2016
Chase Rangel posted a discussion

What is the simplest way to control one brushless motor forward/reverse?

  Hello everyone I don't know much about this type of stuff had only a handful of RC cars.I've been…See More
Dec 12, 2016
Profile IconErving Howe, Chase Rangel, Werner Pretorius and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Dec 12, 2016
Frank Turner is now a member of GarageLab (arduino, electronics, robotics, hacking)
Dec 9, 2016
Marcelo Rodrigues and swapnil are now friends
Dec 6, 2016
Marcelo Rodrigues replied to swapnil's discussion Tiny 85 based Thermometer
"Dear Swapnil, Since you don't have any experience with programming you should consider start…"
Dec 5, 2016
swapnil posted a discussion

Tiny 85 based Thermometer

Hi, can any one guide me how to make the tiny 85 based Thermometer which will glow the LED…See More
Dec 4, 2016
Spyros Svoronos is now a member of GarageLab (arduino, electronics, robotics, hacking)
Dec 4, 2016
K Biju is now a member of GarageLab (arduino, electronics, robotics, hacking)
Nov 29, 2016
Profile Iconswapnil and Yasser joined GarageLab (arduino, electronics, robotics, hacking)
Nov 28, 2016
udit is now a member of GarageLab (arduino, electronics, robotics, hacking)
Nov 25, 2016
Aqeel munir is now a member of GarageLab (arduino, electronics, robotics, hacking)
Nov 23, 2016

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service