Author Topic: analog controlled pwm  (Read 568 times)

m0xek

  • Newbie
  • *
  • Posts: 9
analog controlled pwm
« on: July 18, 2018, 08:51:21 am »
hi, i know very little about arduino so can i get some help please.
i need to input to the digispark 2 analog voltages a and b.

a is representing the current used by my house. b is the current exported to the grid from my solar inverter.

if b is greater than a, then the difference must be output as 0%-100% pwm. this will go to a dump load to use the excess power.

the result should provide a dynamic load that diverts excess energy and keeps my export to zero.

it seems a simple function to me, but is this within the capabilities of a digispark? it has 2 analog inputs and a pwm output, and speed is not an issue as long as it can sample faster than once per second.

is there any BASIC compliler i can use? my programming experience was zx spectrum 1982!

m0xek

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #1 on: July 21, 2018, 11:52:08 am »
bump?  :-[

scruss

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #2 on: July 21, 2018, 03:52:09 pm »
What are the sensors you're using to generate these analogue voltages?

There's no BASIC compiler for Arduino/DigiSpark. It would be worth learning a little about the standard language before attempting this

m0xek

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #3 on: July 24, 2018, 11:43:40 am »
thanks for the reply.i am using two current sensing torrodial coils terminated with resistors to give voltage output. this will be amplified by op amps in precision rectifier mode to give a useful range for the digispark. output pwm will go to a rectified, smoothed load via a big mosfet or igbt.
i already have the sense coils in place and have monitored various load conditions on the scope.

i think this is a good project to learn from as the math and logic is basic. i just need to understand how this is realised in code.

m0xek

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #4 on: July 31, 2018, 01:12:13 pm »
ok i came up with my first program! im very exited but want some opinions as to if its correct.

Code: [Select]
int inputone = 2;
int inputtwo = 3;
int outpin = 1;
int outputdif = 0;
int used = 0;
int generated = 0;

void setup () {
  pinMode (inputone , INPUT);
  pinMode (inputtwo , INPUT);
  pinMode (outpin , OUTPUT);
}

void loop () {
  int inputone = analogRead(used);
  int inputtwo = analogRead(generated);
  if (used  > generated) {
    int outputdif = 0;
  }
  if (used < generated) {
    int outputdif = (generated - used);
  }
  analogWrite (outpin , outputdif);
  }

m0xek

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #5 on: August 02, 2018, 06:57:38 am »
:o :o

thanks for the contructive critisism/

im learning:

Code: [Select]
#define pin1Input  5 //wemos p1 digispark d5 (a0)
#define pin2Input  2 //wemos p2 digispark d2 (a1)
#define pin3Output  1  //wemos p4 digispark p1
#include "DigiKeyboard.h"

float outputdif = 0.0;
float used = 0.0;
float generated = 0.0;


// MAIN SETUP
void setup() {
 
pinMode (pin1Input,INPUT);
pinMode (pin2Input,INPUT);
pinMode (pin3Output,OUTPUT);
Serial.begin(9600);
}


// MAIN LOOP
void loop() {
 
used = analogRead(pin1Input);
generated = analogRead(pin2Input);

//serial console script:
//Serial.print ( "used");
//Serial.print ( used);
//Serial.print("         ");
//Serial.print ("generated");
//Serial.print (generated);
//Serial.print("         ");
//Serial.print ("output");
//Serial.print (outputdif);
//Serial.println ();

//usb text output
DigiKeyboard.print ( "used");
DigiKeyboard.print ( used);
DigiKeyboard.print("         ");
DigiKeyboard.print ("generated");
DigiKeyboard.print (generated);
DigiKeyboard.print("         ");
DigiKeyboard.print ("outputdif");
DigiKeyboard.print (outputdif);
DigiKeyboard.sendKeyStroke(KEY_ENTER);

if (used  > generated) {
  outputdif = 0;
} else if (used < generated) {
  outputdif = generated - used;
}

analogWrite(pin3Output,outputdif);

//delay(1000);
}


m0xek

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #6 on: August 02, 2018, 06:58:43 am »
i am getting issues using pin 5 (a0) causing reset to occur.

m0xek

  • Newbie
  • *
  • Posts: 9
Re: analog controlled pwm
« Reply #7 on: August 02, 2018, 08:33:01 am »
finally fixed it. there seemed to be clashes with gpio function.

this runs:
Code: [Select]
#define pin1Input  2 //wemos p1 digispark 4
#define pin2Input  3 //wemos p2 digispark 3
#define pin3Output  1  //wemos p4 digispark p1
//#include "DigiKeyboard.h"

float outputdif = 0.0;
float used = 0.0;
float generated = 0.0;


// MAIN SETUP
void setup() {
 
pinMode (pin1Input,INPUT);
pinMode (pin2Input,INPUT);
pinMode (pin3Output,OUTPUT);
//Serial.begin(9600);
}


// MAIN LOOP
void loop() {
 
used = analogRead(pin1Input);
generated = analogRead(pin2Input);

//serial console script:
//Serial.print ( "used");
//Serial.print ( used);
//Serial.print("         ");
//Serial.print ("generated");
//Serial.print (generated);
//Serial.print("         ");
//Serial.print ("output");
//Serial.print (outputdif);
//Serial.println ();

//usb text output
//DigiKeyboard.print ( "used");
//DigiKeyboard.print ( used);
//DigiKeyboard.print("         ");
//DigiKeyboard.print ("gen");
//DigiKeyboard.print (generated);
//DigiKeyboard.print("         ");
//DigiKeyboard.print ("out");
//DigiKeyboard.print (outputdif);
//DigiKeyboard.sendKeyStroke(KEY_ENTER);

if (used  > generated) {
  outputdif = 0;
} else if (used < generated) {
  outputdif = generated - used;
}

analogWrite(pin3Output,outputdif*3);

delay(500);
}

i now have gpio 0 and 2 to print to OLED.