readVoltage() reading only 0 or 5

조회 수: 10 (최근 30일)
Gun Min Song
Gun Min Song 2018년 11월 7일
댓글: Álvaro Aparicio Serna 2020년 9월 25일
From an Arduino Mega I am outputting 0,1,2,3,4 volts using writePWMVoltage function (at D13), then I connected D13 to A0 for readVoltage. In theory, I am supposed to get 0,1,2,3,4V from A0, but I am only getting values 0 or 5 from A0. I already checked D13 is outputting correct values by measuring with DMM.
Am I wiring something wrong? Why is this happening? Any help is appreciated :)
  댓글 수: 2
Gun Min Song
Gun Min Song 2018년 11월 7일
This is the code I used for this experiment ----------------------------------------------
clear
clc
a = arduino
for i=0:4
writePWMVoltage(a,'D13',i)
voltage(i+1) = readVoltage(a,'A0')
pause
end
writePWMVoltage(a,'D13',0)
voltage(6) = readVoltage(a,'A0')
Walter Roberson
Walter Roberson 2018년 11월 8일
https://www.mathworks.com/help/supportpkg/arduinoio/ref/writepwmdutycycle.html
Try setting the duty cycle higher in case it is returning to base before you can read it.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rohan Kale
Rohan Kale 2020년 2월 14일
In my understanding you are trying to read the DC voltages from the PWM waveforms generated on D13. Your code sequence seems to be alright. However, the hardware setup is where the problem is originating. In theory, the DC i.e. mean voltage, can be changed by changing the PWM duty cycle. I think the readVoltage API is just picking up the instances where the waveform is either a LOW or a HIGH, that's why you are reading either 0V or 5V. In order to read DC voltages correctly from the waveform, you may want to insert an RC filter between the PWM and analog pins. The RC filter will essentially increase the decay time as the capacitor would now discharge slowly. Try varying time constant of the circuit, may be keep it as 0.1 seconds. This should probably solve your issue.
  댓글 수: 1
Álvaro Aparicio Serna
Álvaro Aparicio Serna 2020년 9월 25일
Very helpful answer, thank you very much.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by