# Can I get rid of my Arduino board and control a solenoid with a square signal generator?

I am using an Arduino board right now with two dials - one controls length of time open and the second controls the duration between openings. I would like to simplify the design and just use a square signal generator that has buttons to adjust duty cycle. I hacked together my first iteration. Would this be possible? If so, what parts would I need to make it work?

• Reprogram the Arduino to use buttons. Alternatively, program a different microcontroller to use buttons as controls and generate the signal.
– JRE
Commented May 10, 2022 at 14:19
• In my opinion, simplest circuit is 555 generator with duty cycle controlled by potentiometer. It's output goes to gate of MOSFET, which controls solenoid. Exact solution depends on your target, could you specify it? Commented May 10, 2022 at 15:22

Whatever you're controlling the system with, what matters is the signal the reaches the controlled object. It doesn't matter what and how generates it. Depending on what device you're using and what kind of frequencies you need. I take it, arduino frequencies are no problem, so we're at something low and easily accessible.

Can you do it with a some square wave generator? Yes. But the problem is in controlling it. How precise can/should it be? Do you have to have dials or will potentiometers do? Or are they too inaccurate (in part due to your hand turning it inaccurately)?

I suggest improved Arduino approach first. Use Atmega328's timers. Two of them, in fact.

You can pick timer counter frequency by setting a prescaler from system clock, then you can set its compare value to such that you have a specific length of your cycle (full cycle). Maximum value (255) should correspond to the longest period of your output waveform that you will ever need (or be larger). Anyway, once the timer counter reaches the compare value, it will generate an interrupt. For example, there is an 8-bit timer with 256 values (0-255), you can set it to 0-255 to decide how long your entire period of output will be.

Example: timer's prescaler is, say, 16. So at 16MHz system clock, timer's clock is 1MHz, and whenever it reaches compare value of, say, 100, it triggers an interrupt. Timer counter goes up every 1us (1MHz timer clock), 100 counts will generate an interrupt. We have just created a regular interrupt every 100us. Set prescaler to value that would satisfy your needs.

Anyway, when the timer generates an interrupt, it toggles the pin and kicks off another timer.

The second timer is supposed to have smaller compare value (or, rather, interrupt period time, if prescaler is different) than the first one. Once it fires an interrupt, it stops and resets itself and togges the output pin.

I've drawn a picture of what I mean:

It may look like a complicated solution at first, but it's a good place to look deeper into how timers work, it's not all THAT compicated, check the datasheet section about timers.

I quickly glanced through the datasheet, I don't think atmega328's timers can work in master/slave configuration, so my solution seems the only relatively reasonable.

As for changing the values, external interrupts are all your friends. Change timer 1 compare value to control the period of the output wave, change compare vaoue of timer 2 to control output low time (in my example)

This solution should have enough accuracy for your purpose. All pin toggle execution delays should be offset in toggle it up and toggle it down, so overall error should be small unless you go close to maximum possible ouput frequencies (still worth checking of course).

• wow! I will digest this and play around. thank you!
– Walt
Commented May 10, 2022 at 17:11