I am attempting to set an analog voltage control signal with a Rasberry Pi 3 B+.
My Python 3 script uses RPi.GPIO
to output PWM across a range of duty cycles to simulate analog output---without the use of any additional hardware. The signal I wish to output ranges between 0.221 and 0.310 Volts.
Examples on the web usually show a call to initialize the PWM at 100 by p = GPIO.PWM(OUT_PIN1, 100);
. Ideally, I would increase this. My code (seen below) runs through a range of PWM values and I can observe voltage changes using a multimeter on the pins.
It appears that at a frequency of 100, the values are slightly high but at a value of 200, the values are much higher than expected and then erratic. Ideally, I would run at the maximum frequency to have as many values between 0.221 and 0.310 as possible but with a frequency of 100 it appears I only have increments of 0.033V and so only ~2-3 actual levels between the two extremes.
Is 100 the maximum value for RPi or should I disregard the multimeter at these high frequencies?
import RPi.GPIO as GPIO
import time
# Cycle Through a range of simulated analog output voltages using PWM
print('GPIO Version', GPIO.VERSION)
print('Setting GPIO to BOARD Mode...\n')
GPIO.setmode(GPIO.BOARD)
OUT_PIN1 = 33 # PWM PIN
OUTPUT_VOLTAGE = 3.3
MIN_VOLTAGE = 0.221
MAX_VOLTAGE = 0.310
BASE_DUTY = MIN_VOLTAGE / OUTPUT_VOLTAGE * 100.0
MAX_DUTY = MAX_VOLTAGE / OUTPUT_VOLTAGE * 100.0
MAX_INC = 5
print('PWM Setup ...')
GPIO.setup(OUT_PIN1, GPIO.OUT)
p = GPIO.PWM(OUT_PIN1, 100);
p.start(0)
print('Running')
while 1:
for k in range(MAX_INC + 1):
newDuty = (MAX_DUTY - BASE_DUTY)/MAX_INC*k + BASE_DUTY
print(k,' Changing to Duty Cycle:', newDuty, '%', ' Target: ', newDuty/100*3.3, 'V')
p.ChangeDutyCycle(newDuty)
time.sleep(3)