1

My project is to use an Arduino to control two solenoid valves with PWM and receive an input signal from a sensor as feedback.

enter image description here

The valves direct the flow of water which cause a hydraulic actuator to move which is detected by an LVDT sensor.

What I want my program to do is to allow me to set a desired displacement as a goal which causes the Arduino to dynamically change the duty cycle of the pwm signal based off of the feedback input the Arduino receives. I am going to implement a PID to control it.

The circuit consist of the Arduino sending PWM signals to two logic level NPN mosfets to control the solenoids that are powered by 2 external 12V sources.

enter image description here

enter image description here

What I specifically need help with is how to write the part of the program that will let me produce 2 PWM signals that will allow me to change the frequency of the pwm waves so that the solenoids have enough time to switch and then also the ability to set the duty cycle.

I am a total novice programmer and its my first time working with electronics. I'm just hoping someone can point me in the right direction.

jsotola
1,5442 gold badges12 silver badges20 bronze badges
asked Sep 27, 2021 at 19:22
1
  • What sort of frequency do you need to run the solenoids at? If it's on the order of 10Hz, 1Hz, or slower, you might consider implementing a software PWM rather than use hardware PWM. Commented Sep 29, 2021 at 0:45

3 Answers 3

4

If you only need two PWM pins, I suggest using hardware PWM with Timer 1. The Timer 1 library makes the process quite easy. It supports frequencies from 0.12 Hz up to a few MHz, although the resolution degrades above 7.8 kHz.

Compared to an interrupt-based solution, hardware PWM has less jitter and it does not consume CPU cycles. The drawback is that you will be limited to 2 channels operating at the same frequency, but this doesn't seem to be an issue for your project.

answered Sep 27, 2021 at 19:49
1
  • I agree, for one or two signals: noting the comment in the sourcecode regarding which pins may be used. I'd also comment that it's to Paul's credit that he respects the license of predecessor code. Commented Oct 27, 2022 at 12:55
0

Tacking on this as a belated observation more than anything else.

Since there is no indication of an accumulator on the hydraulic circuit and the two valves are working in opposition, it might be necessary to maintain both complementary PWM ratios and consistent relative phase.

The TimerOne library appears- by eye- to maintain a consistent relative phase. The AVR_Slow_PWM library definitely doesn't.

If it is /not/ necessary to apply an offset to the PWM edges to accommodate the valves having different open and close times then it might be easiest to simply change the switching sense of one of the FETs with both driven from the same PWM signal (i.e. when one sees 75% on, the other sees 75% off).

If it /is/ necessary to apply an offset then it might end up being best to manually code something in a timer interrupt handler, particularly since I am told that typical valves should operate at about 10Hz but not much faster: this eliminates most of the standard PWM libraries.

answered Oct 28, 2022 at 12:14
0

You can try to use my AVR_Slow_PWM Library, which can provide these following features and generate 2 independent PWM signals to any GPIO pin. Certainly only if your hardware design can correctly use PWM signals.

This library enables you to use ISR-based PWM channels on AVR-based boards, such as Mega-2560, UNO,Nano, Leonardo, etc., using AVR core to create and output PWM any GPIO pin. Because this library doesn't use the powerful purely hardware-controlled PWM with many limitations, the maximum PWM frequency is currently limited at 500Hz, which is still suitable for many real-life applications

Juraj
18.3k4 gold badges31 silver badges49 bronze badges
answered Sep 27, 2021 at 19:28
3
  • Just out of interest: are you using digitalWrite() for your software PWM? Commented Sep 28, 2021 at 9:11
  • Yes. digitalWrite() is used whenever a change is necessary. Commented Sep 29, 2021 at 13:42
  • I've been looking at something similar to this application over the last few days, trying to prepare a skeleton project as a learning exercise for a friend with limited programming experience. While the AVR_Slow_PWM Library looks very good in principle, in practice it desperately needs a simple example generating just one or two signals: nothing clever, just minimal setup and adjustment code similar to the TimerOne library's fan example and definitely not containing any interrupt handler etc. unless this really is necessary. Commented Oct 27, 2022 at 12:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.