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: 1677

Tags: mbed, serial, tutorial

Comment

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

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Profile IconCeline Buline Forma and Gilmar Barbosa joined GarageLab (arduino, electronics, robotics, hacking)
7 hours ago
Gilmar Barbosa replied to GarageLab's discussion Show your lab!
15 hours ago
GarageLab posted blog posts
17 hours ago
Profile IconMichael Tria, Carl C Sartin and Oswaldo Machado joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
GarageLab posted a blog post

Pressure Gauge Modded to Measure Internet Usage

The appeal of having a 100 year old gauge on the wall that shows the current Internet usage is hard…See More
Monday
GarageLab posted blog posts
Friday
Profile IconAngelo Zanellato and Allan Byres joined GarageLab (arduino, electronics, robotics, hacking)
Aug 21
Shannon Bradley commented on GarageLab's blog post Company creates device that recharges smartphones ultrasound
" I have been having good results out too ten to twenty miles. I'm about to fix this…"
Aug 21
GarageLab posted blog posts
Aug 21
Marcelo Rodrigues commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Jay, Here it is. Enjoy! "
Aug 20
Jay Curtis commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Hey, great stuff! Found this site through searching for a solution to get VGA to work with LCD and…"
Aug 20
GarageLab posted a blog post

Omote uses light to project virtual makeup and real-time animations on your face

A team of artists and computer graphics designers have created a projection mapping system that you…See More
Aug 19
Jay Curtis is now a member of GarageLab (arduino, electronics, robotics, hacking)
Aug 19
GarageLab posted blog posts
Aug 18
GarageLab posted blog posts
Aug 14
Tony Warren posted a photo
Aug 13
Dmitriy Zelinskiy joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Aug 12
Profile IconMike Hutchings, Dmitriy Zelinskiy and blake tony joined GarageLab (arduino, electronics, robotics, hacking)
Aug 12
GarageLab posted blog posts
Aug 12
Siti Normah is now a member of GarageLab (arduino, electronics, robotics, hacking)
Aug 11

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service