/*
* This is an example code for a BUCK converter circuit made with arduino
* I've used arduino NANO. We have to set the timer of the PWM on pin D3
* No feedback is connected here
* Subscribe: http://www.youtube.com/c/electronoobs
* webpage: http://www.electronoobs.com/eng_circuitos_tut10.php
*/
int potentiometer = A0; //From the main potentiometer
int PWM = 3;
void setup() {
pinMode(potentiometer, INPUT);
pinMode(PWM, OUTPUT);
TCCR2B = TCCR2B & B11111000 | B00000001; // pin 3 and 11 PWM frequency of 31372.55 Hz
}
void loop() {
float voltage = analogRead(potentiometer);
int VALUE = map(voltage,0,1024,0,254);
analogWrite(PWM,VALUE);
}
hi KlausHi,
There are lot of discussions here and even more in the internet about driving a high side MOSFET.
Thus I recommend to do a search on this topic.
100mH is a rather huge inductance. Are you sure this is the correct value?
1N4001 is a rather slow diode .. and it´s not rated for 3A. Not suitable for your circuit.
There is no voltage feedback in your circuit. .. but the LM2596 has.
You won´t get regulation speed, resolution, PWM frequency like the LM2596.
How "close" do you need to work it like the LM2596?
Klaus
/*
* This is an example code for a BUCK converter circuit made with arduino
* I've used arduino NANO. We have to set the timer of the PWM on pin D3
* No feedback is connected here
* Subscribe: http://www.youtube.com/c/electronoobs
* webpage: http://www.electronoobs.com/eng_circuitos_tut10.php
*/
int potentiometer = A0; //From the main potentiometer
int PWM = 3;
void setup() {
pinMode(potentiometer, INPUT);
pinMode(PWM, OUTPUT);
TCCR2B = TCCR2B & B11111000 | B00000001; // pin 3 and 11 PWM frequency of 31372.55 Hz
}
void loop() {
float voltage = analogRead(potentiometer);
int VALUE = map(voltage,0,1024,0,254);
analogWrite(PWM,VALUE);
}
#include <Arduino.h>
//Analog input pin for the potentiometer
int potentiometer_pin = A0;
// PWM output pin
int pwm_pin = 3;
void setup() {
// Adjusts the pin to input mode
pinMode(potentiometer_pin, INPUT);
// Adjusts the pin to output mode
pinMode(pwm_pin, OUTPUT);
// Adjusts the PWM frequency to 980.39 Hz.
// By default arduino PWM frequency is 490.20 Hz,
// but this value is not high enough to have a stable DC output voltage in low duty cycles.
TCCR2B = TCCR2B & B11111000 | B00000011;
}
void loop() {
// Reads the voltage of the potentiometer to a value between 0 to 1023
// The resolution of ADC on Arduino is 10 bits.
int duty_cycle = analogRead(potentiometer_pin);
// Maps the value from [0 1023] to [0 255] for the PWM function of Arduino
int duty_cycle_mapped = map(duty_cycle, 0, 1024, 0, 254);
// Sets the PWM signal duty cyle value, 0->0%, 254->100%
analogWrite(pwm_pin, duty_cycle_mapped);
}
sure im intersted to send feedback to the MCU but for now i want to change the volt using PWMAs pointed out earlier w/o feedback from output back to Arduino,
using Arduino A/D to read voltage, you do not have a regulator ?
So you do not care that as load changes the output V changes ?
Just curious.
Regards, Dana.
* "it didn`t work" ... means exactly what?i have tested another method & didnt work also
a useful example will be great
hi DanaNote in your diagram your MOSFET symbol seems to have Source and Drain
connections reversed. Eg. the symbol itself. Your labels on diagram correct
however.
View attachment 176972
View attachment 176971
With respect to the latency problem in post #7 you could play with a non linear
filter in lieu of the RC LPF. For example split filter into two sections, one initial LPF
with low RC time constant, followed by a second section with high RC time constant,
and strap diodes across the R in that section. That way when large steps occur in PWM
duty cycle the diodes conduct and rapidly charge the high valued C in that section,
which shortens latency. Once that C charges the diodes turn off appling the full RC
time constant to the circuit. Standard technique used in PLLs to reduce acquisition time.
Lastly of course an I2C DAC with output buffer would easily produce accurate values
where no fdbk to the Arduino would be needed and latency very short. Just a thought.
Maybe even find one with 100 mA type output capability.......Or sue a power opamp
to handle buffer/current.
Regards, Dana.
here is the circuit of lm2596 module which can regulate the output without connecting a loadOther points to note:
1. 1N5819 is the type of diode to use (1N4001 is unsuitable) not the transistor type.
2. to see a change in output voltage you must attach the load, without it the 100uF capacitor will retain the highest voltage placed across it and it will not fall down as the potentiometer is turned.
3. as already pointed out, 100mH is too large to be useful at those frequencies.
If you want to regulate the output voltage, connect a potential divider across the output to scale the voltage to within the ADC range and feed it to a second ADC input. Then, instead of converting the potentiometer voltage directly to PWM ratio, use the difference between it and the divider to decide whether the ratio needs increasing or decreasing. If you want to be clever, check if the output voltage is much lower than it is set to, or is zero to detect whether you have an overload condition or shorted output.
Brian.
LED as well as R1/R2 are connected to the output. -->they are "load"which can regulate the output without connecting a load
We had a lot of questions -- still unanswered.what's wrong with my setup for testing ?
thanks KlausLED as well as R1/R2 are connected to the output. -->they are "load"
We had a lot of questions -- still unanswered.
Hard to help if yo don´t give these informations.
We don´t know how you tested it, what you expect and what you see instead.
Klaus
#include <Arduino.h>
//Analog input pin for the potentiometer
int potentiometer_pin = A0;
// PWM output pin
int pwm_pin = 3;
void setup() {
// Adjusts the pin to input mode
pinMode(potentiometer_pin, INPUT);
// Adjusts the pin to output mode
pinMode(pwm_pin, OUTPUT);
// Adjusts the PWM frequency to 980.39 Hz.
// By default arduino PWM frequency is 490.20 Hz,
// but this value is not high enough to have a stable DC output voltage in low duty cycles.
TCCR2B = TCCR2B & B11111000 | B00000011;
}
void loop() {
// Reads the voltage of the potentiometer to a value between 0 to 1023
// The resolution of ADC on Arduino is 10 bits.
int duty_cycle = analogRead(potentiometer_pin);
// Maps the value from [0 1023] to [0 255] for the PWM function of Arduino
int duty_cycle_mapped = map(duty_cycle, 0, 1024, 0, 254);
// Sets the PWM signal duty cyle value, 0->0%, 254->100%
analogWrite(pwm_pin, duty_cycle_mapped);
}
thanks for the useful informationsyour schematics are inconsistent. Values change form one to the next.
There are free simulation tools. I recommend you to use them. Especially for one with not deep experience they are very helpful to play around with different part values .. to see what happens.
Some basic (mind) calculation:
Your output capacitor is 220uF. A LED current maybe is 10uA. Your switching frequency is about 30kHz (30us)
the basic capacitor formula is: C = I * t / V
Now you have: C, I, t thus you can calculate V
V = I * t / C = 0.01A * 30us / 220uF = 0.3uAs / 220uF = about 0.0015V or 1.5mV
(hopefully my mind calculation is correct. No guarantee for this)
What does this 1.5mV mean:
The capacitor gets charged (30kHz) and discharged (by the LED). In best case - at almost zero duty cycle - the LED is able to drop the capacitor voltage by 1.5mV each cycle.
So if the average current through the coil is just a bit higher than the 10mA then the capacitor voltage will rise to the maximum.
Now how high is the inductor current? There is AC current (often measured in RMS) this can be calculated with frequency and inductance. It is not of interest here. Of interest is the average current. or DC current. And as you know inductors are low impedance for DC. Thus you need onle a very tiny duty cycle to get 15mA of average inductor current.
Sadly you miss to answer about your real coil inductance. As mentioned 100mH seems not to be a realistic value.
I don´t do calculations with this value.
With 30kHz and 8 bit PWM resolution you get the smallest ON_time of 30us/256 which is about 0.12us
Let´s calculate with 100uH (instead of 100mH).
L = V * t / I --> I = V * t / L = 10V * 0.12us / 100uH = 12mA
The average current on a DCM 10V_in 5V_out, 1/256 duty cycle is 12mA *3 / (2 * 256) = 70uA.
This is far below 12mA. Seems to be good.
But if you run a simulation you will see that your MOSFET turns on fast, but very slowly switches OFF. Thus you never get a true ON time close to 0.12us.
Klaus
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?