I have a GPIO that creates the PWM pulse. Would it be possible to have my python code read the same GPIO, and on high execute my code to read the ADC? Would that disrupt the PWM because I'm accessing the pin?

    pi = pigpio.pi()


    lsb = 0.004  # 4.096/1024 
    adc = spidev.SpiDev()
    adc.open(0,0) # SPI 0 with CE_N0
    # max speed in intermal mode
    adc.mode = 0b00

## Read from ADC while PWM'ing
    #process for PWM
    pwm_p=Process(target=tx_pulses, args=(pi, GPIO_PWM, 900, 65535,.5))
             adc.writebytes([0x8E]) # ask for channel 1
             my_bytes = adc.readbytes(2)  # read channel 1
             value = (my_bytes[0] << 3)
             value = value | (my_bytes[1] >> 5)

When I add:


To try and sample on the HIGH of the wave signal, the program stops running which I find strange. I imagine it's because it's turning off the wave while then ends the process. If I can't do this, I was thinking of maybe trying to add an event as when the wave goes high.

  • what do you mean by GPIO?
    – jsotola
    Feb 16 '20 at 1:02
  • 1
    Ah let me see. To "upgrade" a GPIO pin to a PWM pin, I need to (1) set the GPIO pin mode to Output, and then (2) set the GPIO pin already set to output mode to PWM pin, with init conditions freq, duty cycles etc. So a PWM cannot do input and output/PWM a the same time.
    – tlfong01
    Feb 16 '20 at 2:36
  • If that is using the PWM code I gave in another answer are you sure it's running? The code I gave blocked until the wave chain had been sent. You seem to be wanting to read an ADC at high speed with jitter free timing. That is very hard on the Pi. I do offer one method at abyz.me.uk/rpi/pigpio/examples.html#C_rawMCP3008_c Be aware this is not simple code. To adapt it to another ADC requires a reasonably high level of programming abilty.
    – joan
    Feb 16 '20 at 20:44
  • @joan Yea I'm sure it's running because I am scoping the output and I am able to measure. I'll look into your example and see if I can adapt it. IF not, I'll try to look into an alternative. My goal is to read an ADC when the PWM is high.
    – George
    Feb 17 '20 at 6:24
  • With the comment, the ADC is continuously reading when the process of generating the waves is high. I was thinking of trying to use something like the add_event_detect in the RPi lib but I don't know how compatible it is with pigpiod. I haven't tried it yet.
    – George
    Feb 17 '20 at 6:32

Yes you can on a Pi. You can read the current level of a GPIO regardless of its mode (input, output, alt0 to alt5).

For instance look at piscope. It displays the current level of all GPIO, regardless of whether they are inputs, outputs, I2C, SPI, UART, PWM etc.

  • No this won't work. I just tried, and when you try reading it stops the operation of the PWM.
    – George
    Feb 15 '20 at 23:59
  • If you are using pigpio you can read any GPIO regardless of its mode. Just call the read() function. There is no need to set the GPIO as an input (because if you do so that stops the GPIO acting as an output).
    – joan
    Feb 16 '20 at 8:45
  • that's weird, when I tried to do a read() it stopped the PWM functionality.
    – George
    Feb 16 '20 at 17:19
  • I meant to read, while it's generating the PWM at the same time, i.e while it's outputing the PWM signal, I can read if it's high or low.
    – George
    Feb 16 '20 at 17:19
  • What code are you using? Perhaps add a short script which shows the problem.
    – joan
    Feb 16 '20 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.