CubeCell - issues with ultrasonic sensor JSN-SR04T V 2.0

Hi for all.
I try to send the distance that the sensor measures via lorawan. With this code I get the distance without problem:

#define ECHOPIN GPIO0// Pin to receive echo pulse
#define TRIGPIN GPIO5// Pin to send trigger pulse
void setup(){
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
}
void loop() {

digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
distance= distance/58;
Serial.print(distance);
Serial.println(" cm");
delay(2000);// Wait 50mS before next ranging
}

Blockquote
but when I insert the code in the lorawan section, the result of the measurement is wrong, it only reads it if I insert it in the setup of the code for lorawan. Sorry for my English. Everything compiles fine, but it doesn’t read the value correctly inside the void of “prepareTxFrame”

static void prepareTxFrame( uint8_t port )
{

pinMode(Vext, OUTPUT);
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS

delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIGPIN, LOW); // Send pin low again
digitalWrite(Vext, LOW);
nivel = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
nivel= nivel/58;
nivelTX=nivel;
digitalWrite(Vext, HIGH);
digitalWrite(GPIO0, LOW);
pinMode(GPIO0, ANALOG);
uint16_t voltaje = getBatteryVoltage();

unsigned char *puc;
puc = (unsigned char *)(&nivel);

appDataSize = 6;
appData[0] = puc[0];
appData[1] = puc[1];
appData[2] = puc[2];
appData[3] = puc[3];
appData[4] = (uint8_t)(voltaje>>8);
appData[5] = (uint8_t)voltaje;

Serial.print(nivel);

Serial.println(" cm medida primera");
delay(2000);// Wait 50mS before next ranging
Serial.print(nivelTX);
Serial.println(" cm segunda");
}

Can somebody help me ? other sensors work properly within that void.

This has been discussed before, it is an issue with micros() used in pulseIn not working anymore due to a bug which is fixed in the code on github.com

Thanks for the quick reply. Do you mean this code?

#define trigPin GPIO1
#define echoPin GPIO2

float distance;
float pulse_width;

void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}

uint32_t out_flag_1 = 0;
uint32_t out_flag_2 = 0;
uint32_t t1;
uint32_t t2;

void time1()
{
out_flag_1 = 1;
t1 = micros();
}

void time2()
{
out_flag_2 = 1;
t2 = micros();
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
attachInterrupt(echoPin,time1,RISING);

 while(1)
 {
    delayMicroseconds(1);
    if(out_flag_1)
    {
      out_flag_1 = 0;
      detachInterrupt(echoPin);
      delayMicroseconds(1);
      attachInterrupt(echoPin,time2,FALLING);
    }
    if(out_flag_2)
    {
      out_flag_2 = 0;
      detachInterrupt(echoPin);
      break;
    }
  }

pulse_width = t2 - t1;
distance=pulse_width*0.034/2;
// Prints the distance(The unit is centimeter) on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(1000);

}

No I mean in the arduino support libraries, assuming you are developing in the Arduino IDE, when to difficult I have a workaround for you …

ok, now I understand you. is that I do not quite understand why the code works perfectly outside the void and if I put it inside it does not work.

Is your sensor power connected to VEXT?

You can refer to this example: MB1040

thanks for your help. no i use vdd pin

The code does not give an error, but as I said above, it does not measure well, I give you the log of the arduino ide, with the sensor placed 160cm away

joining…joined
distance: 10.16
confirmed uplink sending …
receive data: rssi = -56, snr = 14, datarate = 5
distance: 5.60
confirmed uplink sending …
receive data: rssi = -60, snr = 13, datarate = 5
distance: 0.48
confirmed uplink sending …
receive data: rssi = -58, snr = 13, datarate = 5
distance: 0.48
confirmed uplink sending …
receive data: rssi = -58, snr = 13, datarate = 5
distance: 74212360.00
confirmed uplink sending …
receive data: rssi = -57, snr = 14, datarate = 5
distance: 0.03
confirmed uplink sending …
receive data: rssi = -57, snr = 14, datarate = 5

if i put this code all is ok

#define ECHOPIN GPIO0// Pin to receive echo pulse
#define TRIGPIN GPIO5// Pin to send trigger pulse
void setup(){
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
}
void loop(){

digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
distance= distance/58;
Serial.print(distance);
Serial.println(" cm");
delay(2000);// Wait 50mS before next ranging
}

arduino exit
160 cm
155 cm
160 cm
160 cm
160 cm
160 cm
160 cm
160 cm
160 cm
160 cm
160 cm
160 cm
160 cm

thanks to [weradus] all is ok now.

add this part of code
/* Prepares the payload of the frame */
extern “C” void systemTimer(void); // We need this as a work-around …

static void prepareTxFrame( uint8_t port )

and finish

case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep();
CySysTickSetCallback(4,&systemTimer);
break;
}

tnkas again weradus

1 Like

@weradus I don’t think they changed the micro() function in their new update of the code, they just measure distance without micros() to avoid the problem. See https://github.com/HelTecAutomation/ASR650x-Arduino/blob/master/libraries/Sensor_ThirdParty/examples/MB1040/MB1040.ino

Your workaround is working perfectly though. Thanks again ^^

The example is using pulseIn() this function relies on micros() to work properly … but you are right micros() itself wasn’t changed just some logic around it to keep it working after a deep sleep.

Take a look here

oh yeah @weradus nice one. But I honestly can’t see how their latest commit solves the issue. Would be great if you could explain this.