Getting an ultrasonic sensor to work with a Xenon board

I’m attempting to get a HC-SR04 ultrasonic sensor to work with a Xenon board, but the Xenon only has 3.3V. I’ve been using the usual #include <HC_SR04.h> and hardware wise have been trying to get the voltage down using a 1K and 2K resistor — but I seem to always get either -1 or values that fluctuate (and are pretty much what I’d get without the sensor being there at all!).

Has anyone had any luck getting an ultrasonic sensor to work nicely with the Xenon boards? My guess is that it’s because there’s no 5V on it, but it could be something else I’m missing?

Here’s what I’ve got connected up in my last attempt:

Any help is much appreciated!

Perhaps you can find what you need with this link:

The problem with this link is that the author was using a micro that has 5V tolerant pins but on a Xenon there aren’t any.
So as some of the commenters there stated the current limiting resistor won’t do much for the 5V vs. 3.3V issue but a voltage divider (or a diode as in the last comment in the link) may do better.

But there are other threads in this forum that deal with the same or similar problem.
Maybe you could get in contact with any of the members who may have found a way to make it work

The HC-SR04 will not likely work with a supply voltage less than 5V. To get it to work from the Xenon, you should either use the USB power (5V) or boost the LiPo or 3.3V power with something like this.

The datasheet doesn’t state the limits on the trigger pin so you should assume it requires a 5V signal. That means you will need to shift the trigger from the Xenon up to 5V as well as shift the echo from the sensor down to 3.3V. Each of these is a uni-directional signal so a resistor network will work on the echo line however shifting up is more complex. I prefer the bi-directional level shifter circuit like the one used here. This is very flexible and reliable for testing as long as the switching speeds are adequate for your application.

I've just tested with a couple of HC-SR04 sensors of different sources and they all accepted a 3.3V trigger signal.
But to protect the echo pin on the Xenon you need a voltage divider (or other means of level shifting).

This is a very simple test code that prints the raw timing of the pulses to check whether your hardware is working

// Wiring Sensor Xenon
//        Vcc    VUSB
//        Trig   A0
//        Echo -> 22k -- A1 -- 33k --> GND 
//        GND    GND

bool defState = LOW;
void setup() {
    pinMode(D7, OUTPUT);
    pinMode(A1, INPUT);
    pinMode(A0, OUTPUT);
    digitalWrite(A0, defState);

void loop() {
    static uint32_t ms = 0;
    int t;
    if (millis() - ms < 500) return;
    ms = millis();
    digitalWrite(D7, !digitalRead(D7));
    digitalWriteFast(A0, !defState);
    digitalWriteFast(A0, defState);
    t = pulseIn(A1, HIGH);

you can buy the HC-SR04P with a working rango of 3-5v
i was using this sensor on a 3v board and worked fine

Thanks you so much for that! Would using a 1k and 2k resistor be similar? Or could that be where I’m going wrong? Should I try to get a 22k and 33k resistor as you’ve got mentioned? Or a 3k with a 2k?

I’ll be honest in saying I’m relatively confident in doing a lot with Particle boards, but understanding resistor combos has always eluded me :stuck_out_tongue:

Also a big thank you to everyone who has posted up thoughts and suggestions so far! Will look into the links you’ve provided :slight_smile: I’d consider getting a HC-SR04P but I’ve already got a whole lot of HC-SR04s around from other projects, so I was hoping to just use what I had :wink:

A suggestion is to use this (or similar) that will nicely isolate the 3v3 and 5v sections. power the HV from the VBUS pin and theLV from the 3.3V pin on the Xenon. The extra channels on the level convertor can be use to drive an I2C 5V display …

The important thing for the voltage divider resistors is their ratio.
The voltage across the resistor R2 should not exceed the specified voltage for your GPIO (3.3V).

With a voltage divider like this

5V --- R1 --+-- R2 --- GND

You’d calculate the voltage at GPIO as

V = 5V * (R2 / (R1 + R2))

with 1k & 2k (standad values would rather be 2k2 or 1k8) you’d get 3.43V with 2k2 and 3.21V with 1k8.
2k2 & 3k3 would give you 3V.

And the higher the values the lower the current flowing through the voltage divider and with relatively slow signals the parasitic capacitance shouldn’t play a role.
But for your use case current draw won’t be an issue since you are obviously powering from a USB source.
Only running on battery you’d maybe need to invest a bit more (e.g. boost converter to get 5V and power management).

I got myself a 2k2 and 3k3 resistor to give it another go with your test code, when I run it, I get 0 regularly popping up in the serial console — what’s the expected output of it?

When running my ultrasonic sensor code I had before, I still seem to get no signal from it…

I’ve got things wired up like so:

I’ve also got a logic level converter module, so might try that at some point too maybe?

Would you happen to have an oscilloscope?
That would help greatly to see what signal you actually get from the sensor.

Sadly, I don’t have an oscilloscope! Considering connecting up an Arduino or a Particle board with a 5V to see what happens and compare, because it seems so weird that it’s always reading no values. I’ve used an ultrasonic sensor from the same set in a Raspberry Pi project and it seemed to work, but maybe it was a fluke and others in that set aren’t so good :stuck_out_tongue:

Okay, in an update, if I connect up an Arduino to the power and ground (using the Arduino’s 5V), it picks up the right values a-okay! So it must be an issue with the 3.3V not providing enough power to my ultrasonic sensor (maybe especially so because it’s a cheaper sensor from aliexpress?).

I wonder, are there any movement/motion sensors that work better on 3.3V? Otherwise I might need to see if there’s a nice option for powering things alongside the Xenon board?

Here’s what worked so far:

Since your current SR04s need 5V supply (although they accept a 3.3V trigger signal) this may still be an option for you