Digistump Forums

The Oak by Digistump => Oak Support => Topic started by: brohan on July 27, 2016, 10:14:57 pm

Title: Is this sensor safe for Oak (3.3v)
Post by: brohan on July 27, 2016, 10:14:57 pm
I am working on putting together the Ultrasonic tutorial. I have been working with a sensor (us-100, see link below)  that says it works with 3.3 volts. It appears to me that the distance it measures is accurate (whereas the HC-SR04 5v sensors aren't accurate at 3.3v). My concern is that the input pin isn't receiving more than it can safely handle. How can I check? I've been running my setup for about 20 minutes, and no funny smells.

On a side note, when I contribute to a wiki tutorial, do I need to do the entire entry at once, or can I work on it over a period of days.

Thank you for the input.

Title: Re: Is this sensor safe for Oak (3.3v)
Post by: aztorell on July 28, 2016, 06:12:58 am
As long as you powered the sensor from the regulated 3.3V supply on the Oak, you'll be fine.
Title: Re: Is this sensor safe for Oak (3.3v)
Post by: brohan on July 28, 2016, 06:37:08 am
Thank you. The HC-SR04 info I read showed one needing a voltage divider to protect a 3.3v device, even with giving it 3.3 on the trig pin.
Title: Re: Is this sensor safe for Oak (3.3v)
Post by: PeterF on July 28, 2016, 05:38:20 pm
Yes, if were a HC-SR04-type model that supports running from 3.3v and 5v (like the HC-SR04+), it will be safe to use when run from 3.3v without any level shifting. That particular model appears to a more specialised one, in that it has a jumper on the back that changes the echo and trigger pins to become Serial RX and TX pins, and enables a serial link. You'll need to be very clear in the tutorial that it is not the common ultrasonic sensor that people will encounter / get it starter kits. The normal cheapie unit that people would buy or get in starter kits would need a level shifter (resistive one being the simplest) of some description unless they were lucky enough that it runs from 3.3v.

As far as the wiki, put a note at the top of the page that it is a work in progress and you'll be fine to work on it incrementally.

And for future tinkering, you might find a logic level shifter like one of these handy (http://www.ebay.com.au/itm/Bi-Directional-Level-Shifter-Logic-Level-Converter-8-Channel-for-Arduino-DIY-/141670629695?hash=item20fc3a413f:g:moIAAOSweW5VWvWb) handy to have - and because its bi-directional, it should work with just about any sensor or module you throw at it. You can get cheaper two channel ones (http://www.ebay.com.au/itm/2-Channel-3-3V-5V-4CH-Logic-Level-Converter-TTL-Bi-Directional-Shifter-Module-/271957280186?hash=item3f51eac5ba:g:2DEAAOSwPcVVzc8K), but for just a bit more you can't beat the dedicated 8CH modules. They are always handy to have, especially when you suddenly realise the part you wanted to use has 5v I/Os, and you just want to get the darn thing working! ;)
Title: Re: Is this sensor safe for Oak (3.3v)
Post by: brohan on July 29, 2016, 08:42:13 am
PeterF: As always thank you for the response and insight. I didn't realize there was an HC-SR04+, I grabbed the US-100 as it was available on Amazon Prime, and had the lower voltage specs. I has surmised that one had to use what was included in the starter parts list in the starter tutorial, but was perplexed with the 5v req of the HC-SR04, but no shifter in the kit. I'm excited to put together a tutorial for the Ultrasonic, and will be sure to clearly specify that I am using something other than a standard HC-SR04.

I now have an 8 channel logical bi-directional level shifter on the way to add to what was my small collection of micro-electronics :-).

Title: Re: Is this sensor safe for Oak (3.3v)
Post by: Rando on August 01, 2016, 08:40:02 am

Thanks for the pointer to the + model. I wasn't aware there was a low voltage version the HC-SR04.

Title: Re: Is this sensor safe for Oak (3.3v)
Post by: brohan on August 04, 2016, 04:33:59 pm
I picked up a TXB0108 level shifter to start working with the HC-SR04. Below is how I connected the pins:

OAK   ->   txb0108   <-   hc sr04
3.3vcc->   VCCA      
                 VCCB   <-   VCC
                 GND   <-   GND
3.3vcc ->    OE   
Pin 2 ->      a1      
Pin 4 ->      a2         
                   b1   <-   trig
                   b2   <-   echo

Here is the sketch:

Code: [Select]
 HC-SR04 with TCB0108 level shifter
 VCC to Oak vcc
 GND x2 to Ground Rail to Oak Ground
 Trig to Oak Pin 2
 Echo to Oak Pin 4

int trigPin = 2; // Trigger Pin
int echoPin = 4; // Echo Pin
int LEDPin = 1; // Onboard LED

long duration;
long inches;

char post_Distance[16]; //particle post variable

void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use onboard LED as indicator

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */

 //reset trig pin
 digitalWrite(trigPin, LOW);

//send pulse
 digitalWrite(trigPin, HIGH);
 //stop sending signal
 digitalWrite(trigPin, LOW);
//listen for echo pulse
duration = pulseIn(echoPin, HIGH); 
// Calculating distance by pulse width
 inches = (duration/2) / 74;

//Send distance to cloud for viewing
 sprintf(post_Distance, "%06d", inches); //convert int to string for Particle.publish
  Particle.publish("distance:", post_Distance);
//use onboard LED for visual feedback             
 if((inches < 144) && (inches > 1)) {   // Length effective range (1, 3000).
digitalWrite(LEDPin, HIGH);
 else {
 digitalWrite(LEDPin, LOW);

When I power up the Oak, I still receive all zeros for my distance in the Particle logs.

Is this the correct way to use the level shifter?
Is this level shifter responsive enough to listen to the return echo and report it?

Thanks for helping me understand the level shifter.
Title: Re: Is this sensor safe for Oak (3.3v)
Post by: PeterF on August 04, 2016, 05:18:35 pm
Two things jump out unless I'm misreading your connections...

You need to make sure that GND is connected to all devices - so connect the Oaks GND to the txb0108, and the oak,txb0108 and hc sr04 will all have a common ground (reference). Doesn't matter what the voltage is, generally you want a common ground for all devices. - although it seems from your sketch comment that you have connected ground all the way through...

You've connected the Oaks 3.3v to one side of the shifter, but it doesn't look like you've connected any power to the HC-SR04... the level shifter doesn't provide any power, it just does the translation. You need to connect the low voltage to one side (like you've done with VCCA) and the higher voltage to the other (VCCB). Since you're probably powering the Oak from the USB port, you can use the VIN pin to get the 5v supply for the high side, so just connect VCCB to VIN (if powering from the usb port).

OAK   ->   txb0108   <-   hc sr04
3.3vcc->   VCCA     
VIN ->       VCCB   <-   VCC
GND->       GND   <-   GND
3.3vcc ->    OE   
Pin 2 ->      a1     
Pin 4 ->      a2         
                   b1   <-   trig
                   b2   <-   echo
Title: Re: Is this sensor safe for Oak (3.3v)
Post by: brohan on August 04, 2016, 05:38:00 pm
My limited experience did have me wondering how the chip could take in 3.3 and put out 5, in reference to pluging the VIN to the 5V side of the TXB0108
In my diagram (and reality) I didn't have the Oak grounded. I ran a ground from the Oak to the same breadboard line with the TXB0108 GND and the HR-SR04 GND

Viola, It works. Many, many more wires than using the 3.3v sensor, but doable, and I'm glad for the experience that I will no doubt need.

Thank you sir.

Title: Re: Is this sensor safe for Oak (3.3v)
Post by: PeterF on August 04, 2016, 06:04:14 pm
Lol... yes it does get messy due the need to translate the voltages :D Glad it it's working for you now...