CubeCell - issues with ultrasonic sensor

I want to measure time interval between rising edge from cubecell(Trig) and
falling edge from ultrasonic sensor (Echo) shown below pic.

@johnarnoldbrown

OK. 10uS? One year?
You must have some idea of the range of time values you expect to deal with.

around 1ms-20ms resolution~0.01ms

OK, I’ll try to work something out later.
There is an accuracy problem, mentioned elsewhere in this forum, because the system clock is only good to around ± 2%.

uint16_t thisCount, lastCount, elapsedCount;
pinMode(PWM1,OUTPUT); // Simple way to start the PWM counter timer
analogWrite(PWM1,50); //GPIO2
PWM1_SetPrescaler(PWM1_PRESCALE_DIVBY32); // Gives resolution of approx 2.666 uS, so a max time of about 175mS
lastCount = (uint16_t)PWM1_ReadCounter();

while(1)
{
delay(20);
thisCount = (uint16_t)PWM1_ReadCounter();
elapsedCount = thisCount - lastCount;
Serial.println(elapsedCount);
lastCount = thisCount;
}

In your code, you’ll want to do the 1st PWM1_ReadCounter when you fire off the pulse, and then read the counter again when the reply comes back. I would set up some other timer to expire after 150mS or so, for the times when you don’t get any reflection.

There’s probably a neat way to do this using the input capture mode of the PWMTimerCounter block, but I don’t have the documentation to know how, or even which pin to use.

The only ultrasonic sensor I have on hand is the HC SR04, which is 5V, if I remember correctly.
I will take a look later, I guess I could use a transistor to interface to the CubeCell.
It seems like some of the posters here don’t want to do any of the work for themselves, they just want Lego style programming.

I wouldn’t normally bother, but need something to do while “locked-down”.

perhaps some of the users cant do better.
We all have different priorities here for different kind of sensors.
I dont need ultrasonic sensors i use laser tof sensors. my priority lays there and on other things and we all have limited time.

If you can adopt a library by yourself great.
if not you can ask if anybody can do the job for you, but please be kind and wait for them to finish the work.

Thank you for a try. If any updated ultrasonic library I Will be very grateful

The example for HC-SR04

1 Like

Thank you for updated ultrasonic example, but ‘micros’ was not declared in this scope show on below link. I see in cubecell core I only see millis function. How to add micros function?
@Supporter

Regards,
Bo

Aaron added the function of the micro in the last commit, but this function still not include in the V0.0.5 framework (V0.0.6 will have micros function, but V0.0.6 will release together with ASR6502 projects).

So, can you need to install the development framework via git

Ok, I will try modify 0.05 versions

thanks you

Is this any use to you? Uses 0.04

#include “LoRaWan_APP.h”
#include “Arduino.h”

#define ECHO_PIN GPIO1
#define TRIGGER_PIN GPIO3

#define TIME_OUT (1 << 0)
#define RESULT (1 << 1)

volatile uint16_t startCount;
volatile uint16_t elapsedCount;
volatile uint8_t echoFlags;

TimerEvent_t echoTimeOut;

void echoTimeOutEvent(void)
{
TimerStop(&echoTimeOut); // Kill the timeout timer
detachInterrupt(ECHO_PIN); // detach the interrupt from the echo pin
echoFlags |= TIME_OUT; // Set a flag for the foreground process
}

void leading(void)
{
startCount = (uint16_t)PWM1_ReadCounter(); // Grab the counter contents at the leading/rising edge of echo pulse
attachInterrupt(ECHO_PIN,trailing, FALLING); // Set to grab the falling/trailing edge
}

void trailing(void)
{
elapsedCount = (uint16_t)PWM1_ReadCounter() - startCount; // Work out the elapsed time
echoFlags |= RESULT; // Set a flag for the foreground
TimerStop(&echoTimeOut); // Kill the timeout timer
detachInterrupt(ECHO_PIN); // Detach the interrupt
}

void startPulse(void)
{
// Set up a time out, if we dont’ get an echo within 200mS
TimerInit(&echoTimeOut, echoTimeOutEvent);
TimerSetValue(&echoTimeOut, 200);
TimerStart(&echoTimeOut);

//Send the trigger pulse
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Attch an interrupt to catch the rising edge of the echo pulse
attachInterrupt(ECHO_PIN,leading, RISING);
}

void setup() {
Serial.begin(115200);
delay(50);
BoardInitMcu();
Serial.println(“Setup…”);
analogWrite(PWM1,50); //GPIO2
PWM1_SetPrescaler(PWM1_PRESCALE_DIVBY32); // 2.66uS per count
pinMode(TRIGGER_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
startPulse(); // Trigger the first pulse
}

void loop()
{
if(echoFlags)
{
if(echoFlags & TIME_OUT)
{
Serial.println(“Timed out”);
}
else
{
Serial.println(elapsedCount0.0172.666);
}
echoFlags = 0;
startPulse();
}
}

IT WORKED WITH JSN-SR04T-2.0!!

Aaron Lee updated in GitHub an example using ultrasonic sensor HC-SR04 11 hours ago, but it was not working with JSN-SR04T-2.0.

To use with JSN-SR04T-2.0, I had to change line 50 of the code.
I put 20 instead of 10 microseconds.
Then it worked but it wasn’t really good.
So I put a pull up 10k ohm resistor on echo pin. Then it worked pretty well.

Does anyone know if there is a problem changing that?