Arduino writePWMDutyCycle not performing the cycle

2 Ansichten (letzte 30 Tage)
Min
Min am 20 Sep. 2024
Kommentiert: Umar am 6 Okt. 2024
Hi,
I am having a weird issue with the Arduino via Matlab code.
this is the simple code I have
a = arduino
configurePin(a,'D12','PWM')
writePWMVoltage(a,'D12',3)
writePWMDutyCycle(a,'D12',0.5)
When I run this code, it is just outputting 1.65V which is 0.5 of 3.3V (Due) instead of doing the cycle of the voltage.
Anyone made this cycle to work?
  5 Kommentare
Min
Min am 23 Sep. 2024
I see, it needs to be on and off then the switch is 'On' then that must be in while loop since I have other switches also needs to be excuted. But the 'else' function portion can be without while. Will that help?
Umar
Umar am 6 Okt. 2024

Hi @Min,

Let me understand your issue you are encountering, the code you shared is intended to control a PWM signal on pin D12 of an Arduino board using MATLAB. You first set,a voltage of 3V using writePWMVoltage, then attempts to control the duty cycle with writePWMDutyCycle. However, you observe that the output voltage is consistently 1.65V, which suggests that only the average voltage of a PWM signal (based on duty cycle) is being outputted rather than a stable voltage. As @Anton Kogios noted, writePWMVoltage and writePWMDutyCycle do not operate simultaneously; the latter will overwrite any settings made by the former. This means that after setting a voltage with writePWMVoltage, calling writePWMDutyCycle will change the behavior to generate a PWM signal instead. However, observed output of 1.65V indicates that the average voltage during the PWM cycle is indeed 50% of 3.3V (which is roughly half of the maximum voltage output for a typical Arduino). This aligns with how duty cycles work—if you're setting it to 0.5, you will see an average voltage output based on the duty cycle applied over time. Please bear in mind that frequency of the PWM signal can significantly affect how it is read by measuring devices like voltmeters. If the frequency is too high, standard voltmeters may not capture accurate readings since they typically average voltages over time.

To implement a solution effectively while avoiding infinite loops, consider this approach:

value = C1switch.value;
if strcmp(value, "On")
  while true
      writePWMDutyCycle(a, 'D12', 0.5); % Set duty cycle to 50%
      pause(0.1); % Adjust pause duration as necessary for your 
      application
  end
else
  writePWMDutyCycle(a, 'D12', 0); % Turn off PWM when switch is off
end

I agree with @Walter Roberson comments such as, I would be a bit concerned about the infinite loops. You would need to interrupt the program in order to exit the loops.

Infinite loops can make debugging difficult; therefore, consider implementing an exit condition or a way to interrupt the loop gracefully (e.g., using keyboard input or another condition).

To confirm PWM behavior accurately, try using an oscilloscope or a more responsive voltmeter that can capture fast-changing signals. If Simulink successfully produced expected results while MATLAB does not, ensure that your MATLAB implementation correctly replicates Simulink's timing and configuration settings.

In nutshell, your approach needs adjustment by focusing on either setting up a proper duty cycle without conflicting commands or managing loop conditions effectively to avoid infinite execution. This should help achieve the desired PWM behavior and ensure accurate voltage readings from your Arduino setup. If further issues arise or if you need more advanced features (like varying frequencies), consider exploring additional libraries or configurations within MATLAB's support for Arduino hardware.

Hope this helps. Please let me know if you have any further questions.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by