Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Do Arduino and Circuitpython use PulseIn Differently?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Do Arduino and Circuitpython use PulseIn Differently?

by Daft_vagabond on Thu Oct 21, 2021 2:51 am

I know the obvious answer is "yes" due to fundamental differences between the two.

But

I have a small module designed for making props. It has motion sensors on it, powers LEDs, makes sounds, that sort of thing. It also has a feature where you can have one of the led pins try to emulate whatever the sound is doing by producing a PWM. The idea being that on LED can pulse with an intensity relative to the sound, though not the volume of the sound. I think it's based on the actual analog data (or wave forms, I'm not sure which term is appropriate here) so that volume is irrelevant.

I had a project going on an Arduino and I was using pulseIn to read the pwm, and then take that number and do stuff with it. In general, I would get a range of values between 0-27, the higher values consistently popping up whenever I would do something that would produce a more intense sound.

Code: Select all | TOGGLE FULL SIZE
byte PWM_PIN = 6;  //PWM is coming in here

void setup() {

  pinMode(PWM_PIN, INPUT);
}
void loop() {

  pulse = pulseIn(PWM_PIN, HIGH, 100);

  Serial.println(pulse);
  delay(5);
}



I recently ported the project over to circuitpython on an itsy bitsy m4 express. Reading the documentation for pulseIn, it seems like it's expected to work Identically. That being said, for some reason circuitpython is only returning values 27-28, and not a fluctuation of the two, when I turn everything on, it starts at 0, rapidly ramps to either 27 or 28 and then stays there.

Code: Select all | TOGGLE FULL SIZE
import board
import time
import pulseio

pulse_in = pulseio.PulseIn(board.D13, maxlen=1, idle_state=True)

while True:

    print(pulse_in[0])
    time.sleep(0.05)


I was playing around with it for a bit before posting here so the code above probably isn't EXACTLY what I was using, but something close. maxlen is set to one because I really only ever need to know the most recent value. And I thought about maybe clearing the value after every use of it, but since there's only one space available for storage, it should force the old one to eject, right?

Just in case I had somehow ruined something, I connected it back to the arduino and ran a test with the old code, but it only acted how I expected it to, producing a variation of numbers between 0-27-ish based on what the sound was doing. So it's not my hardware, at least on the module/arduino end of things. Not to say it's the M4. It's likely my code, but I'm not certain what I'm doing wrong.

The only circuitpython tutorial I could find that involved pulseIn was the IR remote one, but unfortunately, reading through that didn't help much.

I also keep gett
"IndexError: PulseIn index out of range"

but I'm only ever storing one value, so calling to the first slot should never be out of range, right?

I'm going to keep reading through the documentation to see if I missed anything, but feel free to tell me the things I'm doing wrong.

I just want read one number at a time and do something based on that number, so I really don't need to store more than one.

Daft_vagabond
 
Posts: 43
Joined: Thu May 28, 2020 2:53 pm

Re: Do Arduino and Circuitpython use PulseIn Differently?

by Daft_vagabond on Fri Oct 22, 2021 12:54 am

I finally got it to work, sort of, using the following code. But there is something odd I want o point out that I think it'd be worth either explaining to me or looking into.

Code: Select all | TOGGLE FULL SIZE
import board
import pulseio
import pwmio
import time

pulse_in = pulseio.PulseIn(board.D13, maxlen=1, idle_state=True)

while True:
    while len(pulse_in) == 0:
        pass
    if pulse_in[0] >= 2 and pulse_in[0] <= 4:
        print(pulse_in[0])
    elif pulse_in[0] >= 5 and pulse_in[0] <= 8:
        print(pulse_in[0])
    elif pulse_in[0] >= 9 and pulse_in[0] <= 19:
        print(pulse_in[0])
    elif pulse_in[0] >= 20 and pulse_in[0] <= 21:
        print(pulse_in[0])
    elif pulse_in[0] >= 22 and pulse_in[0] <= 23:
        print(pulse_in[0])
    elif pulse_in[0] >= 24 and pulse_in[0] <= 27:
        print(pulse_in[0])

    print(pulse_in[0])
    time.sleep(0.1)
    pulse_in.clear()


For some reason the values read by circuit python are more-or-less inverted compared to those read by arduino ide. Arduino will start at zero and as the sound gets more intense, it will increase the value. Circuitpython starts at around 27-28, where arduino would cap-off, and goes down to 6-3 as the sound gets more intense. But what's weird is that circuit python still displays no signal as a 0. What's also weird is that during moments of intense sound, arduino would still randomly dip into lower values very briefly, and circuitpython does that same, but dipping to low values in circuitpython SHOULD mean something entirely different, given that they're inverted.

Do I have something odd in my code? Is there a reason it seems inverted? Can I use a pull up/down resistor to help avoid random dips?

Daft_vagabond
 
Posts: 43
Joined: Thu May 28, 2020 2:53 pm

Please be positive and constructive with your questions and comments.


cron