Hi,
Ok, here is the entire code.
/****************************************************************************************/
/* Simple Arduino regulated power supply 2V to <= 19,9V output with high voltage input >= 10V to 30V max.
* Input/Output voltage difference should 10V not exceed for better output ripple.
* With actual given voltage divider, output voltage may not exceed 19,9V!!!
*
* Attention: Don't adjust output voltage via outputVoltSetPoint higher than 5V without voltage divider, to
* protect your Arduino analog input! Only max. 5V at all the pins are allowed!
* Power MOSFET needs cooling while powering > 0,25A!
*
* Reference:
*
*
* Origin Author: Olaf Meier
* Modified by:
*
* Hardware connection: Attention! Minimum load is needed to discharge output capacitor!
* Connect Gate of MOSFET to Collector of NPN T1 via 10k, not bigger due to C-load of MOSFET
* Connect Basis of T2 via 10k to pin 7 for voltage control
* Connect U_Out, Drain of Q1 to A2 for voltage measurement via divider
*
* Minimum load always needed!
*
* * ToDo:
*
*/
/****************************************************************************************/
/*
* Example of output: In normal state HIGH-Level min. 8uS in steps of 4uS, LOW-Level is variable,
*
*
*/
/****************************************************************************************/
/****************************************************************************************/
/*** Declaration of global constants and initialization of variables. Add includes. ***/
/****************************************************************************************/
/*** Software release and date ***/
//const char* author = "Olaf Meier";
//const char* revision = "R.1.0 ";
//const char* date = "2013/08/31″;
#if ARDUINO < 100
#include <WProgram.h> // Add other libraries on demand as requested
#else
#include <Arduino.h> // Needed when working with libraries
#endif
#include <stdio.h>
/*** Declare constants and variables for the adjustable voltage regulator ***/
const byte actualValueVoltPin = A1; // Measure regulated voltage
const byte controlPin = 11; // PWM for output voltage control
/*** Declare constants and variables for analog input measurement ***/
const unsigned long refVolt = 5000; // Reference voltage default 5V (5000mV) to analog converter; change to 3300 if 3,3V
/*** Don't adjust output voltage higher 19.500mV = 19,5V with existing resistor divider ***/
/*** Regulation does not work higher 20V!!! ***/
/////////////ADJUST VOLTAGE SETPOINT AND DURATION HERE////////////////////////////////////////////////////////////////////////////////////////
const unsigned int outputVoltSetPoint = 7000; // Adjust output voltage (Set 0000 to 5000 in mV, depending on voltage divider)
const unsigned int duration = 180; //duration in seconds
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int actualValueVolt = 0; // Initialize measured output voltage with 0mV to start with/*** Declare constants and variables for resistor voltage diviver at the analog input ***/
const unsigned int voltageDivider = 4; // Value depending on ratio of the resistor voltage dividerbyte
byte PWMValue = 0 ; // Initialize PWM value
unsigned long time;
long Interval = 1000;
int row = 0;
int duration_m = duration*1000; //duration in milliseconds
/****************************************************************************************/
/****************************************************************************************/
void setup() {
//////Turn Pull-up Resistor on/off/////////////////////
pinMode(1+14, INPUT); //both lines, (x+14, ...) x=PinNumber
digitalWrite(1+14, LOW); //HIGH -> Resistor on
delay(100);
//////end adjust pull-up/////////////////////////////////
pinMode(controlPin, OUTPUT); // Pin to control the output voltage
digitalWrite(controlPin,HIGH); // Switch on MOSFET to drive current to the output RC
Serial.begin(128000);
Serial.println("CLEARDATA");
Serial.println("LABEL,Time,millis,set,voltage,pwm");
} // End of void setup() loop
/****************************************************************************************/
/****************************************************************************************/
void loop() { // Function Loop
/*** Calculate mV based on the 10-bit AD-Converter values from 0..1023 ***/
unsigned long currentMillis = millis();
actualValueVolt = ((refVolt * 1000) / 1023) * (analogRead(actualValueVoltPin)) / 1000;
actualValueVolt *= voltageDivider; // Multiply measured analog in voltage depending on divider factor
/*** Output voltage regulation ***/
if((actualValueVolt < outputVoltSetPoint)&&(PWMValue<255)) // Switch on MOSFET while increasing HIGH time of Digital Pin 6
analogWrite(controlPin,PWMValue++);
if((actualValueVolt > outputVoltSetPoint)&&(PWMValue>0))
analogWrite(controlPin,PWMValue--); // Reduce HIGH time of the PWM signal
/////transfer via PLX-DAQ////////////////////////////
Serial.print("DATA,TIME,");
Serial.print(currentMillis);
Serial.print(",");
Serial.print(outputVoltSetPoint);
Serial.print(",");
Serial.print(actualValueVolt);
Serial.print(",");
Serial.println(PWMValue);
row++;
delay(10);
}
//////to see in Arduino Monitor////////////////
/*if(outputVoltSetPoint>0){
Serial.print("Set=");
Serial.print(outputVoltSetPoint);
Serial.print(" mV | ac.V=");
Serial.print(actualValueVolt);
Serial.print(" mV | PWM=");
Serial.print(PWMValue);
Serial.println();
//}
//else {
// Serial.print("something is not right...");
//}
} //end if loop////////////////////
*////////////////////////////////////////////////
I have replaced the 220R Resistor indicated as R1 on my original circuit diagram with a 10k resistor.
I currently have the Arduino plugged into the 12V power supply. Previously I had it connected to the computer and was able to read current PWM signals and the current voltage which was supposedly being output (Vout). This output voltage did not correspond with the voltage I measured with the DMM. I stopped having the Arduino connected to the computer because later I want it to function without the computer and I thought if I set it up that way now (without the connection to the computer) it would be one less variable to think about.
I currently have the V_out+ and V_out- connected to an anode and cathode in a jar of water. I can see bubbles rising, so electrolysis is taking place, and yesterday I measured 3uA current with 8V output voltage... but perhaps this isn't a proper load? I previously had an LED as a load, and then also had it in parallel with the 35V 1000uF capacitor. I had seen this configuration on another circuit diagram on which I was basing my own. I think that same capacitor is now still in my current circuit as a left-over from that previous set-up.
Should I remove that capacitor? or perhaps set up a proper load? would an LED be sufficient? and if I set up an LED as the load, should it be with the capacitor in parallel?
With the current setup (R1 as 10k and capacitor still in place) the output voltage, measured with the DMM, still increases proportionately to the input voltage and does not seem to be affected by the PWM signal... it's certainly not going towards 7V as is being requested in the code.
Thanks for all the feedback and suggestions!
Marcus