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:
/*
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);
delay(100);
//send pulse
digitalWrite(trigPin, HIGH);
delay(10);
//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);
}
delay(1000);
}
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.