Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 11 months 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 !
5 Answers
10 years, 11 months 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.
10 years, 11 months 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 :
- include "mbed.h"
- include "C12832_lcd.h"
- 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 ?
10 years, 11 months 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:
_spi(p5,NC,p7),_reset(p6),_A0(p8),_CS(p11),GraphicsDisplay(name)
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.
7 years, 8 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
https://developer.mbed.org/users/martinsimpson/code/Nucleo_SFR05_SFR04/
posted by 17 Mar 2017
can't*
posted by elith choo si rui 05 Dec 2013http://mbed.org/users/prabhuvd/code/HCSR04/wiki/Homepage
posted by Erik - 05 Dec 2013