# Covert from analog to digital control using arduino

#### MAINAHNOUD

Joined Jul 24, 2019
66
How can I convert this circuit to digital using arduino ?
It has two stage : the first generate triangular wave then generate pulses

#### Attachments

• 187.1 KB Views: 68

#### kaindub

Joined Oct 28, 2019
45
What are you trying to achieve?
With Arduino you can getPWM out of it just by program.

#### Irving

Joined Jan 30, 2016
1,070
If my math is correct that generates a square wave at 14Hz, but there's no variable element there.

Arduino generates PWM natively at about 500Hz so if duty cycle is the only important thing then one line of code will replace that circuit.

If frequency is important too then the code for your needs isn't difficult but we need more info to give you more guidance.

#### RPLaJeunesse

Joined Jul 29, 2018
139
With 1M Ohm and 60pF the RC time constant is 60us. Oscillator period will be in the vicinity of 100us -150us (sorry, not going to canculate it) so 6-10 KHz, way more than 14Hz. There is info online to change the PWM frequency up from the native Arduino Atmega328 PWM values (480 & 960 Hz). It requires about 2-3 added lines of code to change the counter's clock prescaler from 64 to a lower number, typically 8 or 1. Take a look at https://arduinoinfo.mywikis.net/wiki/Arduino-PWM-Frequency

#### Alec_t

Joined Sep 17, 2013
11,813
If my math is correct that generates a square wave at 14Hz
Did you mean 14kHz?

#### Irving

Joined Jan 30, 2016
1,070
I did mean kHz but it should be 12kHz.

time-period for voltage change from 33% to 66% and vice versa is 0.7RC so frequency is 1/(2 * 0.7 RC) = 10e6/(1.4 x 60 x 1) = 11.9kHz

I did it in my head quickly before and used 0.6 rather than 0.7 as the multiplier which gives 13.8kHz...

Doh!

But does it need to be such a high frequency? That can take a lot of resources out of a Arduino, even interrupt driven, depending on what else is required.

Last edited:

#### MAINAHNOUD

Joined Jul 24, 2019
66
If my math is correct that generates a square wave at 14Hz, but there's no variable element there.

Arduino generates PWM natively at about 500Hz so if duty cycle is the only important thing then one line of code will replace that circuit.

If frequency is important too then the code for your needs isn't difficult but we need more info to give you more guidance.
Yes the frequa
If my math is correct that generates a square wave at 14Hz, but there's no variable element there.

Arduino generates PWM natively at about 500Hz so if duty cycle is the only important thing then one line of code will replace that circuit.

If frequency is important too then the code for your needs isn't difficult but we need more info to give you more guidance.
Yes the frequency is important
Could you tell me in steps what should I do?

#### Irving

Joined Jan 30, 2016
1,070
Yes, I can. But we need more information about what you're doing with this. Normally with PWM it's the duty cycle (on/off ratio) that's important, not frequency. Such high frequencies are normally associated with power conversion which is a very specialised area andyou wouldn't generally use a uController to generate it. Why is frequency important here? What are you using the PWM for?

#### MAINAHNOUD

Joined Jul 24, 2019
66
Yes, I can. But we need more information about what you're doing with this. Normally with PWM it's the duty cycle (on/off ratio) that's important, not frequency. Such high frequencies are normally associated with power conversion which is a very specialised area andyou wouldn't generally use a uController to generate it. Why is frequency important here? What are you using the PWM for?
I use pulses signal to switch mosfet

#### Alec_t

Joined Sep 17, 2013
11,813
And what is the MOSFET controlling? If it's a motor or lamp or solenoid then normally the PWM frequency would be a good bit lower than 12kHz.

#### MAINAHNOUD

Joined Jul 24, 2019
66
And what is the MOSFET controlling? If it's a motor or lamp or solenoid then normally the PWM frequency would be a good bit lower than 12kHz.
it charge battery 2v and the frequency in range 12khz

#### Irving

Joined Jan 30, 2016
1,070
Ah, so this is part of a battery charger set-up. An Arduino is the wrong approach here, except as a supervisor for a proper SMPS chip that is better able to manage the PWM and has appropriate loop control for constant voltage and/or current regulation and protective current limit sensing.

Can you explain what you want to achieve overall? What sort of battery chemistry & voltage/capacity, what is the battery supplying power to, and what is it charging from?

#### MAINAHNOUD

Joined Jul 24, 2019
66
I want to convert analog control to digital control to consume less power cause opamp is hungry power device. Also, my system is energy harvesting so the power consumption is more important

#### Irving

Joined Jan 30, 2016
1,070
How much power is the opamp taking really? An Arduino or any adequately fast uController solution will take much more. What is your power budget?

#### Alec_t

Joined Sep 17, 2013
11,813
What are you harvesting energy from? Many sources are such low power that any battery charging current would be miniscule, so PWM would hardly be justified.

#### Irving

Joined Jan 30, 2016
1,070
What are you harvesting energy from? Many sources are such low power that any battery charging current would be miniscule, so PWM would hardly be justified.
Yes, especially as that circuit given by the OP, if using the specified op amp, uses < 10uA @ 6v! 60uW is hardly 'power hungry' and there's no way a digital solution will be any better.

#### MAINAHNOUD

Joined Jul 24, 2019
66
How much power is the opamp taking really? An Arduino or any adequately fast uController solution will take much more. What is your power budget?
Power budget =300uW

#### MAINAHNOUD

Joined Jul 24, 2019
66
Yes, especially as that circuit given by the OP, if using the specified op amp, uses < 10uA @ 6v! 60uW is hardly 'power hungry' and there's no way a digital solution will be any better.
Is there opamp with 10uA?

#### MAINAHNOUD

Joined Jul 24, 2019
66
What are you harvesting energy from? Many sources are such low power that any battery charging current would be miniscule, so PWM would hardly be justified.
From piezoelectric, yes it low power 300uW

#### Irving

Joined Jan 30, 2016
1,070
I think you need to explain the wider system to us. Its impossible to advise on something this complex in a piecemeal fashion.

The opamp you've got there takes 200nA. The 1M resistor chains and the capacitor charging takes close to 6uA, the rest is an estimate of switching losses and drive to the next stage. I'd imagine the switching losses in the MOSFET and inductor (if this is an smps ) will be a bigger problem.

Suggest you look the new ranges of nanoWatt all-in-one smps chips from the likes of TI and Maxim designed for wearable solutions as a better option...