Low frequency square wave in arduino

Hello,

I want to generate a 0.01Hz square wave in Arduino. With that signal I want to power a RC series circuit to charge and discharge the capacitor. I want to measure the current and the voltage and export that through PLX-DAQ to an Excel spreadsheet and graph the current and the voltage depending on time to see the evolution of the two.

I have a 2200uF capacitor and a 1kohm resistor. The time constant is 5RC = 11 seconds. That's why I need such a low frequency signal.

How do I do that? Attach me the code and the PLX-DAQ output please.

5x1000x0.0022 = ?

As for writing your code for you, you'll find some guidance here:

The forum has a for-hire section where you can pay someone to do the work for you, should you wish.

Also, do yourself a favor and calculate the power dissipation in that resistor, just in case.

What time constant?

What is the "5" for?

Maybe easier to use arduino serial.print and monitor to view your data (at least for starters).

is this a test circuit? are you trying to measure the capacitance?

why a fixed frequency, why not switch the input between charge/discharge based on the voltage across the capacitor?

Why not just simulate the square wave in excel and feed it to the calculations for that RC circuit that give you the current and voltage?

See BlinkWithoutDelay in the IDE examples.

1 Like

0.01Hz is 100 seconds, 50 HIGH, 50 LOW. I believe you want 0.05Hz, 10 seconds HIGH, 10 LOW. This simple sketch generates a 0.05Hz square wave on "outputPin" with Arduino Nano.

void setup()
{
  //Serial.begin(9600);
  pinMode(outputPin,OUTPUT);
}

void loop()
{
  static unsigned long timer = millis();
  unsigned long elapsed = millis() - timer,
                interval = 20000UL, // 20 sec
                highTime = interval / 2; // 10 sec
                 
  if(elapsed >= interval)
    timer += interval;
  digitalWrite(outputPin,elapsed <= highTime);  

}
1 Like

In the blink without delay sketch you will just need to change the following line:

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

With 'interval' set to 1000ms it generates a 0.5Hz signal (1s high, 1s low), so you would have to set it to 50000ms to give 0.01Hz.

1 Like

It's no problem, the maximum voltage across the resistor is 5V.
Max. power = (5^2/1000)W = 25mW.

deaythlyhallo,

There is a topic pinned to the top of the 'Project Guidance' section.
It is called:
Demonstration code for several things at the same time.

Using the techniques demonstrated there you could do two things:

  1. Generate your 0.01Hz signal to charge/discharge your capacitor.

  2. Use an Analogue input to measure the voltage on the capacitor, say every 0.1s.
    And then print the results to the serial monitor/serial plotter.

Or maybe something in between. After 5 time constants (=11s) the voltage has risen/fallen to 99.3% of its final value. After 20s it will be fully charged according to an analogRead().

I'd suggest 20s high, 20seconds low = 0.025Hz.

Good point. We await the OP's response with baited bated breath. :grin:

Driving this with an Arduino output? So 100 ohms into essentially a short circuit? Sounds like 50 mA, quickly decaying. Exceeding pin max specs, I'd say.
The power calculation was "just in case" for the education of the poster. 5V @50 mA is a quarter watt instantaneous, so although decaying away, the initial wattage is significant, but agreed, the decay saves him.
But, whatever.

The resistor is not 100 ohms but 1K. Current is thus 5mA max. No problem there. Or did I miss something ?

Never mind. Morphed work I was doing into the OP's thread. Apologies, all.
...walks away, shaking his head.

You'll have to do it in software. Even with the highest prescale and 16-bit timer you can only get about 0.25 Hz (4 seconds).

Here's a way of doing it.

-jim lee