8 years, 1 month ago.

HC SR04 ultrasonic sensor to mbed

hi ,

i have this problem , i can seems to make SR04 to work when connected to mbed micro controller . i have a few qns to ask , how do you connect SR04 to mbed?? which pin to which pin??? what is the code to use to make it work ?

need help urgently .

thanks !


posted by elith choo si rui 05 Dec 2013

5 Answers

8 years, 1 month ago.


8 years, 1 month ago.

Try the SRF05 library in the components section. Connect it as in the example or adjust the example to fit your embed. Its is quite easy to do.

8 years, 1 month ago.

i tried using that . but it doesnt work .

i've then connected it in this way : mbed > sensor gnd > gnd vu > vcc p25 > trigg p6 > echo

and my code :

  1. include "mbed.h"
  2. include "C12832_lcd.h"
  3. include "hcsr04.h"

HCSR04 usensor(p25,p6); C12832_LCD lcd;

int main() {

unsigned int dist; while(1) { usensor.start(); wait_ms(500); dist=(usensor.get_dist_cm()*(343/20000));

lcd.cls(); lcd.locate(0,0); lcd.printf("cm:%ld",dist );

} }

however , it doesnt seems to work . and LCD display nothing . but then i unpin the gnd connections , the lcd shows what i've input in the code (lcd.printf("cm:%ld",dist );) is this why i cant display anything on the lcd? is it that i have to connect the vcc and gnd to a power source ?

8 years, 1 month ago.

Well I can't seem to make comments anymore.

Your problem is the LCD lib. That one has as problem that it doesn't show which pins are connected (it is forked to a library which does show it), and is specifically for the application board. If you look at the code however:


In other words, p6 is already used by it. Try connecting your p6 to another (unused) pin. There should be no special requirements, any regular pin should do.

oh alright thanks !!! i'll try again ! thanks for your help

posted by elith choo si rui 09 Dec 2013
4 years, 10 months ago.

Would anyone kindly post a very simple code to detect hcsro4 sensor connect with frdm board.......

Published this program with lots of comments in code to explain


posted by Martin Simpson 17 Mar 2017