Ultrasonic distance sensor

External hardware connected to the STM32 board
Post Reply
User avatar
RogerClark
Posts: 7683
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Ultrasonic distance sensor

Post by RogerClark » Sat May 09, 2015 9:24 am

Notes on using an ultrasonic distance sensor

Image

The one I have, as 4 connections . Vcc, Trigger, Echo and Gnd

I tried this module on 3.3V, but it doesn't work. It needs 5V.

However it is possible to use this as a 5V device, as long as you connect the echo pin to a 5V tollerant pin on the STM32.
The reference for which pins is still on the old Leaflabs site http://leaflabs.com/docs/hardware/maple-mini.html

I tried to use the NewPing library, but it only works for AVR. The old Ping library may work, but I've not tried it.

This is a simple sketch that does work with my ultrasonic sensor

Code: Select all

#define TRIGGER_PIN    PB12
#define ECHO_PIN       PB13

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(TRIGGER_PIN,OUTPUT);// trigger pin
  pinMode(ECHO_PIN,INPUT);// echo pin
}

void loop() {
  digitalWrite(TRIGGER_PIN,HIGH);
  delay(1);
  digitalWrite(TRIGGER_PIN,LOW);
  
  uint32_t pulseLen  = pulseIn( ECHO_PIN, HIGH );
 
  Serial.print("Distance "); 
  Serial.print(pulseLen/58.138f);
  ;Serial.println(" cm");
  delay(500);
}

thakshak
Posts: 26
Joined: Fri May 08, 2015 6:07 am

Re: Ultrasonic distance sensor

Post by thakshak » Sat May 09, 2015 5:10 pm

RogerClark wrote:Notes on using an ultrasonic distance sensor

Image

The one I have, as 4 connections . Vcc, Trigger, Echo and Gnd

I tried this module on 3.3V, but it doesn't work. It needs 5V.

However it is possible to use this as a 5V device, as long as you connect the echo pin to a 5V tollerant pin on the STM32.
The reference for which pins is still on the old Leaflabs site http://leaflabs.com/docs/hardware/maple-mini.html

I tried to use the NewPing library, but it only works for AVR. The old Ping library may work, but I've not tried it.

This is a simple sketch that does work with my ultrasonic sensor
hi roger,

i tried your new pulseIn and I'm happy to see it working..
(don't know why... its not working with pins PAxx)

and its good for upto 1mtr(100cm) but after that its not accurate(its bad)...

for eg..

my room length is around 6mtrs.
from one side of the room i started measuring the distance using this sensor...
its showing 139~140cms.
so, i stood in front of the sensor and started moving towards the wall...
as i move and reached 140cms... from here how much i move back it still shows 140cms...

i'm using 5v supply..

=============================================

edit:
sorry for that i guess its a sensor issue...
changed the sensors
below are the readings from sensor to object at dist. ~230-240 with another 2 sensors...

Code: Select all

Distance 138.52 cm
Distance 234.01 cm
Distance 234.84 cm
Distance 139.81 cm
Distance 139.81 cm
Distance 181.83 cm
Distance 139.79 cm
Distance 234.49 cm
Distance 234.48 cm
Distance 234.05 cm
Distance 140.67 cm
Distance 140.22 cm
Distance 232.81 cm
Distance 136.83 cm
Distance 142.83 cm

Code: Select all

Distance 233.69 cm
Distance 233.20 cm
Distance 234.51 cm
Distance 138.57 cm
Distance 232.77 cm
Distance 236.61 cm
Distance 245.62 cm
Distance 232.79 cm
Distance 234.10 cm
Distance 143.40 cm
Distance 234.06 cm
Distance 234.48 cm
Distance 232.74 cm
what about that fluctuations to ~140 in that readings...?
but all these sensors gave good readings with my RPi2..

=================================================
edit2:

3.3v don't work...

used 3.6v
readings..

Code: Select all

Distance 233.13 cm
Distance 233.15 cm
Distance 233.58 cm
Distance 235.23 cm
Distance 234.87 cm
Distance 236.15 cm
Distance 138.89 cm
Distance 141.47 cm
Distance 139.74 cm
Distance 233.93 cm
Distance 232.70 cm
Distance 234.42 cm
Distance 233.98 cm
Distance 233.96 cm
Distance 138.03 cm
i'm also using a voltage divider R1=1k and R2=2k on echoPin

User avatar
RogerClark
Posts: 7683
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Ultrasonic distance sensor

Post by RogerClark » Sat May 09, 2015 8:59 pm

Did you use 5v with the sensor on RPI?

I find these sensors very inaccurate.

I monitored mine with an oscilloscope and the issue was not pulseIn but was the data from the sensor

Do you use a library with the sensor for RPi, its possible it includes some data filtering.

It may also be worth looking at the NewPing library, which is AVR Arduino only.
It may be doing some processing of the data

Re Not working on some pins.
Did you check if they were 5v tolerant ?

thakshak
Posts: 26
Joined: Fri May 08, 2015 6:07 am

Re: Ultrasonic distance sensor

Post by thakshak » Sun May 10, 2015 6:17 am

RogerClark wrote:Did you use 5v with the sensor on RPI?
yes
Do you use a library with the sensor for RPi, its possible it includes some data filtering.
no libraries... i just used the code from this page...
http://www.modmypi.com/blog/hc-sr04-ult ... spberry-pi
It may also be worth looking at the NewPing library, which is AVR Arduino only.
It may be doing some processing of the data
will do...
Did you check if they were 5v tolerant ?
yes, just now checked... they are tolerant... and have seen in some other forum that except PB5 every other pin are 5v tolerant.

User avatar
RogerClark
Posts: 7683
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Ultrasonic distance sensor

Post by RogerClark » Sun May 10, 2015 6:43 am

Are you sure it doesnt work on PA pins. Which ones did you try

It just uses basic GPIO, digitalRead. Nothing fancy

So I'm surprised it didnt work on PA

But there could be a bug somewhere

thakshak
Posts: 26
Joined: Fri May 08, 2015 6:07 am

Re: Ultrasonic distance sensor

Post by thakshak » Sun May 10, 2015 8:58 am

RogerClark wrote:Are you sure it doesnt work on PA pins. Which ones did you try

It just uses basic GPIO, digitalRead. Nothing fancy

So I'm surprised it didnt work on PA

But there could be a bug somewhere
i'm sorry... i got confused with the pin labeling on my board (reversed echo and trigger)...

it works with every PA PB PC pins on my board...
sorry once again..

User avatar
RogerClark
Posts: 7683
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Ultrasonic distance sensor

Post by RogerClark » Sun May 10, 2015 10:21 am

No worries

Its easily done. At least you didnt blow any thing up ;-)

User avatar
rexnanet
Posts: 209
Joined: Wed Mar 16, 2016 10:34 am

Re: Ultrasonic distance sensor

Post by rexnanet » Wed Mar 22, 2017 4:10 pm

There's now a 3.3V version of the module. It's named "HC-SR04P".

https://www.aliexpress.com/item/HC-SR04 ... 59780.html

Post Reply