[McHUG] McHUGging at Vail - part 2

Rich Mitchell geobra at att.net
Tue Dec 8 18:14:05 EST 2009


I have updated the work bench that I have been using by eliminating the external power supply.  The first thing I did was to provide 5+ volts to the voltage divider circuit  input (with the diode) from pin 5 to the 10k pot and saw similar results with vDiv/vIn varying from 15 to 75% as the voltage is lowered.  This used the digitalWrite command.  I then tried to eliminate the pot by providing PWM to pin 5 using the analogWrite command and varying the PWM value from 0 to 255.  This was fed to the voltage divider circuit using through about 2350 ohms and connected to ground with 10K pf cap (RC circuit).  This seemed to be the best combination with the components I had on hand.  I could vary the voltage number from about 282 input to 89 with a percentage change from 47 to 97.

I also added a switch at pin 7 so that the sketch can run either pot or pwm with no change.  If switch is high, it will run pwm else the 10k pot. Here is the new code:

/* 
  This version is switchable between constant digital voltage and 
  analog PWM voltage for the 'external' supply.
  This creates an Electronic Workbench to output to an LCD through
  serial communications.  It reads v oltage at two points and 
  divides the larger by the smaller.
 
 */
int VoltIn;
int VoltDiv;
int VoltComp;
boolean isPWM;
boolean increment;
int pwmCnt;
int pwmSwitch;

#define vIn 1   // full voltage is at analog input pin 1
#define vDiv 0  // divider voltage is at analog input pin 0
#define vOut 5  // output voltage is at pwm pin 5
#define sw1 7   // switch for pwm mode

void setup(){
  Serial.begin(9600);    // 9600 baud is chip comm speed
  pinMode(vOut, OUTPUT); // set vOut for PWM output
  pinMode(sw1, INPUT);   // set vOut for PWM output
  delay(1000);
  increment = true;
  pwmCnt = 0;
  pwmSwitch = digitalRead(sw1);  // read switch to see if pwm
  if (pwmSwitch == HIGH) {
    isPWM = true;
  } else {
    isPWM = false;
  }
}

void loop(){
  
  if (isPWM) {                   // do the following if pwm is true
    analogWrite(vOut, pwmCnt);   // send pwm voltage
    if (increment) {             // increment pwm depending on direction
      pwmCnt = pwmCnt + 5;
    } else {
      pwmCnt = pwmCnt - 5;
    }
    if (pwmCnt > 250) increment = false;  // set the direction
    if (pwmCnt < 5) increment = true;
    Serial.print("P, ");    // label
    delay(100);                  // delay 100ms between sends
    Serial.print(pwmCnt, DEC);   // display pwm count in decimal format
    delay(100);                  // delay 100ms between sends

  } else {                      // do the following if pwm is false
    digitalWrite(vOut, HIGH);   // provide 5+ volts for test circuit.
  }

    // write out the value of the voltage input
  VoltIn = analogRead(vIn);        // read the input voltage
  Serial.print(" I, ");    // label
  delay(100);                  // delay 100ms between sends
  Serial.print(VoltIn, DEC);   // display input voltage in decimal format
  delay(100);                  // delay 100ms between sends

    // write out the value of the voltage divider
  VoltDiv = analogRead(vDiv);     // read the division voltage
  Serial.print(", D, ");    // label
  delay(100);                  // delay 100ms between sends
  Serial.print(VoltDiv, DEC);  // display division voltage in decimal format
  delay(100);                  // delay 100ms between sends

      // write out the percent of divided voltage to input voltage
  VoltComp = VoltDiv * 100 / VoltIn;   // calculate percent of dvision voltage
  Serial.print(", C, ");    // label
  delay(100);                  // delay 100ms between sends
  Serial.println(VoltComp, DEC);   // display division percentage in decimal format
  delay(500);

}

73
Rich, N3III


--
McHUG - Physical Computing ;)
MicroController Ham User Group


More information about the McHUG mailing list