How to read Servo pulse signal with Circuit Python on a Rasp

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
thompsman
 
Posts: 31
Joined: Sat Feb 25, 2017 2:57 pm

How to read Servo pulse signal with Circuit Python on a Rasp

Post by thompsman »

Hi,

I want to read a pulse generated from another microcontroller generated for a servo motor using a Raspberry pi Pico GP0 using Circuit Python. I want to save the result in a variable.

I am a novice. Any help greatly appreciated.

Thanks,

Bill

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to read Servo pulse signal with Circuit Python on a

Post by mikeysklar »

Bill,

Take a look at pulseio.PulseIn for measuring pulse lengths on the Raspberry Pi with CircuitPython.

Code: Select all

import pulseio
import board

pulses = pulseio.PulseIn(board.D7)

# Wait for an active pulse
while len(pulses) == 0:
    pass
# Pause while we do something with the pulses
pulses.pause()

# Print the pulses. pulses[0] is an active pulse unless the length
# reached max length and idle pulses are recorded.
print(pulses)

# Clear the rest
pulses.clear()

# Resume with an 80 microsecond active pulse
pulses.resume(80)
docs:
https://docs.circuitpython.org/en/lates ... io.PulseIn

User avatar
thompsman
 
Posts: 31
Joined: Sat Feb 25, 2017 2:57 pm

Re: How to read Servo pulse signal with Circuit Python on a

Post by thompsman »

Thank you for your reply.

I tried the code and don't see anything showing up in the serial window. I changed the code to reflect that I used GP0 on the pico. I also followed the instructions for the IR Sensor tutorial and was able to see data collected and shown using the REPL, so I know I have a signal, and am using the correct pin name.

I will carefully test the code again this evening. Below are two code samples. I notice the first does not have the maxlen, or idle_state. If not included are the default values maxlen = 1 and idle_state = True?

pulses = pulseio.PulseIn(board.D7)
pulses = pulseio.PulseIn(board.D2, maxlen=200, idle_state=True)

Thanks again,

Bill

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to read Servo pulse signal with Circuit Python on a

Post by mikeysklar »

What is the pin pulse length you are sending in?

Which version of CircuitPython and library bundle are you running?

There was a issue with long pulses on the RP2040 that appears to be resolved. I think reviewing the example code provided might help with syntax. Can you post your full code since you have modified it?

https://github.com/adafruit/circuitpython/issues/4659

I can't tell what the default values are for idle_state and maxlen (reviewed source and docs). Probably best to manualy set them with the constructor initialization.

User avatar
thompsman
 
Posts: 31
Joined: Sat Feb 25, 2017 2:57 pm

Re: How to read Servo pulse signal with Circuit Python on a

Post by thompsman »

Hi again,

Thank you so much for your help. With the sample code you sent, (https://github.com/adafruit/circuitpython/issues/4659), I finally got it to work.

I reduced the above code and modified it. Below is the resulting code. I left my initial import statements commented out, as I was surprised it did not work until modified as shown.
I was also surprised that the code did not display the print statements with out the sleep statement. The other thing I was not expecting was that regardless of idle_state= True or False, the pulse data included both the pulse duration followed by the duration between pulses. Changing the idle_state only changed which displayed first in the array. In the code below, I only printed every other element in the array to select out the elements of the array that were the duration between servo pulses.


from board import GP0
from pulseio import PulseIn
from time import sleep

#import pulseio
#import time
#import board

servo_pulses = PulseIn(GP0, maxlen=100, idle_state=False)

#without this sleep the data does not print to the screen

sleep(1.0) # giving myself enough time to connect to the serial

for i in range(1,len(servo_pulses)/2):
print(i," ",servo_pulses[2*i])

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”