gfxgfx
 
Please login or register.

Login with username, password and session length
logo
 
gfx gfx
gfx
19681 Posts in 1259 Topics by 5012 Members - Latest Member: theodorosgrigoriou September 23, 2017, 07:12:38 AM
*
gfx* Home | Help | Login | Register | gfx
gfx
Breaktru Forum  |  eCigarette Forum  |  Modding  |  Topic: A basic Arduino PWM build for beginners.
gfx
gfxgfx
 

Author Topic: A basic Arduino PWM build for beginners.  (Read 17722 times)

0 Members and 2 Guests are viewing this topic.

Offline Zanderist

  • Full Member
  • ***
  • Joined: Feb 2015
  • Location: SI, NY
  • Posts: 40
  • Karma: +5/-0
  • Gender: Male
A basic Arduino PWM build for beginners.
« on: April 29, 2015, 09:32:24 PM »
Seeing someone's interest in using an Arduino, I quickly pieced this together using the examples the IDE came with and added in some extra bits. I made assumptions that a person reading this can already piece together a basic Mosfet box mod.

Here is the Code, remove that Serial.print stuff if you don't need to troubleshoot. Feel free to edit this code and adapt it to your needs. This is extremely basic coding.
Just Copy and paste and upload
Quote
const int analogInPin = A3;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to
const int RedLED=8;
const int YellowLED=9;
const int BlueLED=10;
const int switchpin=7;
 int switchstate=0;

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  switchstate=digitalRead(switchpin);
 
  if(switchstate==1){
analogWrite(analogOutPin, outputValue);
  }
  if(switchstate==0){
    analogWrite(analogOutPin, 0);
  }

if(outputValue>=180){
digitalWrite(RedLED,HIGH);
digitalWrite(YellowLED,LOW);
digitalWrite(BlueLED,LOW);
}
if(160<outputValue&&outputValue<180){
digitalWrite(RedLED,LOW);
digitalWrite(YellowLED,HIGH);
digitalWrite(BlueLED,LOW);
}
if(160>=outputValue){
digitalWrite(RedLED,LOW);
digitalWrite(YellowLED,LOW);
digitalWrite(BlueLED,HIGH);
}

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.print(outputValue);   
  Serial.print("\t switch = ");
  Serial.println(switchstate);

  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);                     
}

Here is a picture of the wiring set up. The pot is 5k in my case though I do feel you could using any pot vaule.


Video of it in Action


What's supposed to happen in this set up is you adjust the pot and an individual LED will light up to give an idea of what temperature the vape will be, Red is HOT, Yellow is WARM, blue is COOL. The push button is to turn on the analogue write function.

A challenge to offer is to incorporate a 5 button press hibernate.
« Last Edit: April 29, 2015, 10:08:25 PM by Zanderist »

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #1 on: April 30, 2015, 11:20:01 AM »
excellent, that helps me a lot. the last time i programmed was when i had an amiga 500 and that was basic. i'll certainly give that a shot.  With arduino would i be able to measure the voltage or would i need to use an rc filter first.

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #2 on: April 30, 2015, 02:16:20 PM »
I would lower the frequency if you're only using a current limiting resistor on the gate. :)

@shandy27  Using an RC filter/software filter will give you the mean.  If you want it to read the output voltage accurately you need to calculate RMS voltage. 
« Last Edit: April 30, 2015, 02:21:03 PM by norcalreballer »

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #3 on: April 30, 2015, 02:47:26 PM »
am i right in saying to get the rms i multiply the peak by duty cycle squared.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #4 on: April 30, 2015, 08:01:00 PM »
Vrms = sqrt((duty cycle/255) * Vbat^2)

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #5 on: May 01, 2015, 11:10:36 AM »
thanks for that, i'll start on it tonight when the wee man is in bed.

Offline Zanderist

  • Full Member
  • ***
  • Joined: Feb 2015
  • Location: SI, NY
  • Posts: 40
  • Karma: +5/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #6 on: May 01, 2015, 12:39:22 PM »
I've done another code that is the next level up, in this verison the battery voltage is sampled while it is not under load. I learned something important from this to, that you cannot read a voltage while it it is under load due to the voltage drop. I don't know what to do go into detail on so just ask me to explain something. The code I've posted is not final production level code, this is still a work in progress. The main goal I have in mind is to use every pin on the arduino so I can maximize its use when I go to build a final copy.

This version offers voltage level LEDS , as well as three different modes that are set by voltage.
bathighvoltage()-runs the device with out change turns on green high voltage LED
batmediumvoltage()-fades the PWM signal over time to conserve power(maybe?), turns on Red LED, lowvoltage one
batlowvoltage()-blinks the low voltage LED(RED)



To read the battery voltage you need to set up a voltage divider circuit, refer to a google search on how to do this in great detail
http://www.electroschematics.com/9351/arduino-digital-voltmeter/

Any one finding this thread for the first time...
DO NOT DIRECTLY CONNECT THE BATTERY INTO THE ANALOG PIN UNLESS THE VOLTAGE IS LESS THAN SUPPLY VOLTAGE.
Quote
//pieced together and coded by the Zanderist(AWA)
 
 int analogInPin = A3;  // Analog input pin that the potentiometer is attached to
 int analogOutPin = 3; // Analog output pin that the LED is attached to
 int analougePin=A2;//battery level read point
 int hvoltage=5;//pin for high voltage LED
 int lvoltage=4;//pin for low voltage LED
 int RedLED=8;//pin for red level LED
 int YellowLED=9;//pin for yellow level LED
 int BlueLED=10;//pin for blue level LED
 int switchpin=7;//pin for switch
 int switchstate=0;//state of the switch
 int mode=0;//sets which voltage mode to run
 

 int levelshutdown=0;//int used as boolean to conserve power
 int fadedone=0;//int used as boolean to stop fadding
 int readenable=1;//int used as boolean to enable battery voltage sample.

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)



int heatpwm=0;//new pwm value for batmedium.
int heatpwminc=-5;
int samplepwm=0; //take a reading of the PWM

int outputPWM=0;


int voltageValue=0;//for voltage reading
int VoutputValue=0;//for voltage computation

float Rratio=0.4;//divider ratio
float vout=0;//voltage out of the divider
float vin=0;//approx voltage at battery
void setup() {
  // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 pinMode(RedLED, OUTPUT);
 pinMode(YellowLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(hvoltage, OUTPUT);
  pinMode(lvoltage, OUTPUT);
  pinMode(switchpin, INPUT);
 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  // check the battery voltage
 voltageValue=analogRead(analougePin);
 vout=(voltageValue*4.68)/1024.0;
 vin = vout/Rratio;
 
  //read switch
  switchstate=digitalRead(switchpin);
 //////
  if(readenable==1){
  outputPWM=outputValue;
  if(vin>7.51){mode=2;}
  if(6.99<=vin&&vin<=7.50){mode=1;}
  if(vin<6.98){mode=0;}
  }
 
 
  if(switchstate==1){
   readenable=0;
    if(mode==2){
      LEDS();
    bathighvoltage();}
   
   if(mode==1){
     if(fadedone==0){
     LEDS();}
    batmediumvoltage();}
 
 
   if(mode==0) {
   
    batlowvoltage(); }
  }

 
  if(switchstate==0){
    readenable=1;
    samplepwm=1;
    analogWrite(analogOutPin, 0);
   turnoffLEDS();
    fadedone=0;
  }



 
 
 


 // print the results to the serial monitor:
 
   Serial.print("heatpwm= \t" );                       
  Serial.print(outputPWM); 
Serial.print("enable= \t" );                       
  Serial.print(readenable);     
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.print(outputValue);
 Serial.print("Voltage=\t");
 Serial.print(vin); 
  Serial.print("\t switch = ");
  Serial.println(switchstate);

  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);                 

}
/////////////////////////////\
void bathighvoltage(){
 
 
digitalWrite(hvoltage,HIGH);
analogWrite(analogOutPin, outputPWM);

}
/////////////////////////////\
void batmediumvoltage(){
 
  if(samplepwm==1){heatpwm=outputValue;samplepwm=0;}
 
  if(fadedone==0){
 
  digitalWrite(lvoltage,HIGH);
   
   
  if(heatpwm>0){
  heatpwm=heatpwm+heatpwminc;
   analogWrite(analogOutPin,heatpwm);
  }
  if(heatpwm<=0){fadedone=1;turnoffLEDS();analogWrite(analogOutPin,0);}
 
   
 
 

 
 
}
}

/////////////////////////////\
void batlowvoltage(){

digitalWrite(lvoltage,HIGH);
delay(250);
digitalWrite(lvoltage,LOW);
delay(250);}
/////////////////////////////
void LEDS(){
if(outputValue>=180){
digitalWrite(RedLED,1);
digitalWrite(YellowLED,0);
digitalWrite(BlueLED,0);
}
if(160<outputValue&&outputValue<180){

digitalWrite(RedLED,0);
digitalWrite(YellowLED,1);
digitalWrite(BlueLED,0);
}
if(160>=outputValue){

digitalWrite(RedLED,LOW);
digitalWrite(YellowLED,LOW);
digitalWrite(BlueLED,HIGH);
}
}
/////////////////////////////
void turnoffLEDS()
{
digitalWrite(RedLED,0);
digitalWrite(YellowLED,0);
digitalWrite(BlueLED,0);
digitalWrite(hvoltage,LOW);
digitalWrite(lvoltage,LOW);
}
« Last Edit: May 01, 2015, 01:15:22 PM by Zanderist »

Offline Mayor

  • Jr. Member
  • **
  • Joined: Apr 2015
  • Posts: 7
  • Karma: +0/-0
Re: A basic Arduino PWM build for beginners.
« Reply #7 on: May 01, 2015, 04:13:37 PM »
Zanderist, thanks for the tutorial. Seems easy enough to follow along with. I've only been learning for a couple weeks and haven't messed with much sensor input stuff besides the brief IDE example about serial printing input value from a potentiometer so this looks helpful for me.

Really awesome forum here

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #8 on: May 01, 2015, 05:09:18 PM »
You should check voltage both open circuit and under load.  The reason is that you don't want cell voltage to drop below a certain voltage at any time.  Minimal voltage is 3V per cell for LiPos, but high drain 18650s can go lower.  The HE2s can actually go down to 2.5V, but the rest are 2.7V.  Though you shouldn't take them down that low since it increases wear and does not garner much extra run time.  3V for 18650s and 3.2V for LiPos is ideal. 

Minimal voltage specification is for voltage under load, not open circuit.  You'll find that when you take a cell down to the minimum under load, it will climb back up in voltage an amount after it's been sitting idle.  It can be as much as a half volt depending on how heavy the load.

What you can do is curve the battery yourself which sounds difficult but it's not.  Discharge graphs only show voltage under load and you need open circuit battery voltages for your fuel gauge.  Just charge up the battery fully then discharge it in precise 15 to 30 second intervals with a known current.  Take a reading after the load is removed and you'll get a set of plot points for open circuit voltage versus battery capacity. 

It would be possible to gauge a battery under load if you know the battery's DC resistance and the load's amperage draw.  In that case you can add current times DCR to the voltage reading then index it to your graph for open circuit voltage versus charge capacity.  You can find a battery's DCR on the bench pretty simply by measuring voltage drop with a known current.  You should measure it at about 50% capacity since it goes down somewhat with a full charge and goes up somewhat as the battery approaches discharged.

Curving a battery yourself can yield a very accurate fuel gauge.  On my own mods, it's pretty darn close.  Checking against a hit counter I get a pretty consistent number of hits for every 10% drop in capacity shown by the fuel gauge.

Oh, I should point out that your circuit is not actually going to measure true open circuit voltage since your electronics are going to draw some current.  However a properly designed MCU system should not draw much so it's in effect open circuit.  On my own mods, the circuit is drawing less than 10mA when taking an open circuit battery measurement, not enough to make any measurable difference.  If for some reason your circuit draws a lot of current when taking a battery measurement you would have to take that into account.

« Last Edit: May 01, 2015, 05:46:57 PM by CraigHB »

Offline Zanderist

  • Full Member
  • ***
  • Joined: Feb 2015
  • Location: SI, NY
  • Posts: 40
  • Karma: +5/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #9 on: May 01, 2015, 06:41:27 PM »
You should check voltage both open circuit and under load.  The reason is that you don't want cell voltage to drop below a certain voltage at any time.  Minimal voltage is 3V per cell for LiPos, but high drain 18650s can go lower.  The HE2s can actually go down to 2.5V, but the rest are 2.7V.  Though you shouldn't take them down that low since it increases wear and does not garner much extra run time.  3V for 18650s and 3.2V for LiPos is ideal. 

Minimal voltage specification is for voltage under load, not open circuit.  You'll find that when you take a cell down to the minimum under load, it will climb back up in voltage an amount after it's been sitting idle.  It can be as much as a half volt depending on how heavy the load.

What you can do is curve the battery yourself which sounds difficult but it's not.  Discharge graphs only show voltage under load and you need open circuit battery voltages for your fuel gauge.  Just charge up the battery fully then discharge it in precise 15 to 30 second intervals with a known current.  Take a reading after the load is removed and you'll get a set of plot points for open circuit voltage versus battery capacity. 

It would be possible to gauge a battery under load if you know the battery's DC resistance and the load's amperage draw.  In that case you can add current times DCR to the voltage reading then index it to your graph for open circuit voltage versus charge capacity.  You can find a battery's DCR on the bench pretty simply by measuring voltage drop with a known current.  You should measure it at about 50% capacity since it goes down somewhat with a full charge and goes up somewhat as the battery approaches discharged.

Curving a battery yourself can yield a very accurate fuel gauge.  On my own mods, it's pretty darn close.  Checking against a hit counter I get a pretty consistent number of hits for every 10% drop in capacity shown by the fuel gauge.

Oh, I should point out that your circuit is not actually going to measure true open circuit voltage since your electronics are going to draw some current.  However a properly designed MCU system should not draw much so it's in effect open circuit.  On my own mods, the circuit is drawing less than 10mA when taking an open circuit battery measurement, not enough to make any measurable difference.  If for some reason your circuit draws a lot of current when taking a battery measurement you would have to take that into account.



The curve idea you bring up sounds like it can be an easy experiment to set up with an arduino.

I'm looking for a one size fits all with the voltage part.

This mod I have set up is set so you can remove the batteries, I haven't yet figured out how to incorporate a charging circuit just yet.

Problem I had though was when I went to fire the coil with this build, the voltage went from 8 volts to 5 volts and since I had tied functions to battery voltage level it went haywire. This led to me sampling voltage during open circuit conditions.

What I've done for any issue with the voltage reading (not posted as of yet) was I added an offset to make it equal to my mutlimeter, so far so good with it.

What trying to do at the moment is remove any square waves with bumps or spikes, so far high value capacitors seem to be doing the trick. Its sad to say though that I don't have any proper training in the subject so I'm just guessing values of capacitance.
« Last Edit: May 01, 2015, 06:47:54 PM by Zanderist »

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #10 on: May 01, 2015, 06:53:38 PM »
If you have room on the screen you can just display the battery voltage. But I'm lazy like that :p

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #11 on: May 01, 2015, 07:45:26 PM »
i was going to ask you earlier how your project was coming on.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #12 on: May 01, 2015, 07:56:36 PM »
It's coming along a lot better now I've done what I should have done in the first place. I've got breakout boards for all the SMD components so I can breadboard it properly and debug the hardware.

I've now got the current sensor and it's amplifier working well enough together. I've been through half a dozen of those .96" OLEDs which all seem to have their quirks, but I've got some I'm happy with. I just need to hook up the digipot and raptor to the breadboard and current sensor and I'll be ready to order another set of boards from OSHPark :).

I'm going to do a PWM next I think, the arduino and raptor are just too big together and PWM will cut the size down by quite a bit. I can re-use a huge amount of my code for it too.

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #13 on: May 01, 2015, 08:10:07 PM »
yeah pwm seems to be the way to go.  It's funny though a few months ago people were saying how crap pwm mods are now everyone wants one lol.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #14 on: May 01, 2015, 08:16:32 PM »
Small and cheap way of buck regulating and at high enough frequency not to get the rattlesnake. A decent 3S LiPo will do it all from low to high power :).

Offline Zanderist

  • Full Member
  • ***
  • Joined: Feb 2015
  • Location: SI, NY
  • Posts: 40
  • Karma: +5/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #15 on: May 01, 2015, 09:34:26 PM »
If you have room on the screen you can just display the battery voltage. But I'm lazy like that :p

Do you need to add additional programming for that?

I see it reads ohms, do you need extra hardware or does the display do it all for you?

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #16 on: May 02, 2015, 04:12:43 AM »
It all has to be coded. The screen is just that - a screen. It's an SPI SSD1306 .96" OLED. I'm using the U8glib library which is excellent (https://code.google.com/p/u8glib/)

To measure ohms I'm using a current sensor then doing the maths with the output voltage. I'm considering using a constant current source for my next project though, unless anyone knows a better way ;).

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #17 on: May 02, 2015, 04:13:47 PM »
If you have room on the screen you can just display the battery voltage.

Yeah you can, but then you only know when you need to charge, pretty hard to tell how much run time you have left based on voltage since it's not really a linear relationship.  It's kind of linear but not on either end of the curve.

I'm considering using a constant current source for my next project though, unless anyone knows a better way.

The problem with regulating current the analog way is you need to maintain some reference voltage which means wasted power.  To convert any voltage regulator to a current regulator requires you maintain a voltage reference typically between .8 and 1.2V.  For high currents it results in a lot of wasted power.

You can regulate current digitally in lieu of voltage or wattage.  I've thought about coding a current regulated mod.  There are some benefits to maintaining a constant current over a constant wattage or voltage.  I haven't seen anyone do it yet and there are no mods off the shelf that can do it either.  To regulate current digitally is only a matter of coding which is no more or less efficient, assuming you are giving up the power to monitor current dynamically already (using a current sense resistor or hall effect sensor)

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #18 on: May 02, 2015, 05:00:11 PM »
Fair comment about the battery monitor, but as I say I'm lazy and tend to keep my batteries fairly well topped up anyway so voltage is a good enough indicator for me.

I think you misunderstood what I was going to use the constant current for. As you guessed I'm using a hall effect sensor (ACS710 although I probably should have used an ACS712 for size, or suggestions for alternatives welcome) to monitor the current and I'm measuring the voltage, with these I can calculate the resistance. I was thinking that another way to calculate resistance would be to use a constant current IC to send maybe 200mA through the coil and measure the voltage drop across it. Any other ideas for measuring coil resistance would of course be welcome.

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #19 on: May 02, 2015, 05:36:22 PM »
Oh okay, I thought I may have missed the mark with that last reply. 

In electronics there's always at least three different ways to do the same thing, usually more.  You're basically describing an Ohmmeter which is a perfectly legitimate way to check resistance.  Using a MOSFET inline you can drop in a voltage divider for a few milli-seconds and use voltage across a reference resistance to find atomizer resistance.  That's how Ohmmeters generally work.  The down side is you can't check current dynamically which is okay I suppose, but personally I prefer to keep tabs on current demand.  It's just safer to insure you never try to provide more current than the circuit can handle.

In terms of complexity, the Hall effect chip you're using is going to be the most simple way to find resistance (in terms of part count).  There's a proper name for calculating resistance with a current and voltage measurement (4 wire or Kelvin measurement), but it's a highly accurate way to do it and you get the benefit of dynamic current monitoring as well.

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #20 on: May 02, 2015, 05:41:27 PM »
probably a stupid question but can hall effect sensors be used to measure the load of a pwm mod like this.

Offline Ian444

  • Jr. Member
  • **
  • Joined: Sep 2014
  • Location: QLD, Australia
  • Posts: 23
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #21 on: May 02, 2015, 05:57:21 PM »
can hall effect sensors be used to measure the load of a pwm mod like this.

Have a look here:
http://breaktru.com/smf/index.php/topic,1385.msg17524.html#msg17524

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #22 on: May 02, 2015, 06:04:33 PM »
Sensing for PWM is essentially the same as sensing for DC.  The primary difference is you need to add your RMS calculations after you take a sample.  PWM does add a level of complexity as I stated in the post linked above.  The main problem being the need to filter noise, but not so heavily you introduce a lot of error.

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #23 on: May 02, 2015, 06:06:43 PM »
I need to upload a video I recorded.  My setup was a mess and I got it soooo close to the target voltage in my tests. :)  It doesn't rattlesnake even at 122hz, btw.  I'm using a shunt monitor in my project. 

EDIT: https://www.youtube.com/watch?v=EZLUMTNEzQg&feature=youtu.be

This was my first test, so be gentle... I'm working on a custom PCB now.  :)
« Last Edit: May 02, 2015, 06:16:10 PM by norcalreballer »

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #24 on: May 02, 2015, 06:19:06 PM »
thanks for the links and help everyone it's much appreciated, my original plan was to use a shunt resistor and an ad623 instrumentation amp to measure the current. i don't plan on doing anything fancy just something to display voltage, wattage, resistance and current.

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #25 on: May 02, 2015, 06:41:37 PM »
That's all I use, just a current sense resistor and standard op-amp.  Though because I use an inexpensive general purpose op-amp I have to calibrate each build.  Using an instrumentation amp or other precision op-amp can eliminate that need, but if you want accuracy to two decimal places, you'll probably still need to calibrate.  You'll probably get accuracy out of the box down to one decimal place, but doubtful you can get it to two without some calibration.

The Hall effect sensors are really nice, very low insertion loss and simple to interface.  However, they are not a widely available part.  There's only one maker I know of.  Also they are not programmable.  You're limited to a set of offerings like 5A, 10A, 20A etc and then the signal range is not adjustable.  You can potentially lose a lot of ADC resolution.  Other than that it's a simple and elegant solution.

Offline shandy27

  • Sr. Member
  • ****
  • Joined: Jun 2012
  • Location: falkirk
  • Posts: 50
  • Karma: +2/-0
Re: A basic Arduino PWM build for beginners.
« Reply #26 on: May 02, 2015, 06:55:09 PM »
am i right in thinking that i would insert an rc filter between the current  shunt and the op amp.

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #27 on: May 02, 2015, 07:08:48 PM »
Yes you always need to filter ADC inputs, either hardware or software.  I use a 3k resistor and a 1u capacitor, but that's for DC.  With PWM you need a lower time constant, probably like 1k and 100n.  Depends on PWM frequency and minimal duty cycle.   Or you can do it in software, sample at maximal rate (usually 100 to 500ksps) during the pulse and average.

If you don't understand what I mean by "time constant" read the replies after the one linked to a few replies earlier.

For a hardware filter, you only need to filter output from your current sensor at the ADC input for your MCU.  You don't need to filter anywhere else and you don't want to since it will likely cause problems with your sensor.  It's tricky to filter inputs on op-amps, it tends to throw them out of spec.


« Last Edit: May 02, 2015, 07:21:38 PM by CraigHB »

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #28 on: May 02, 2015, 07:41:51 PM »
That ends up giving you the mean,  though. 

Offline Zanderist

  • Full Member
  • ***
  • Joined: Feb 2015
  • Location: SI, NY
  • Posts: 40
  • Karma: +5/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #29 on: May 02, 2015, 09:19:57 PM »
One annoying thing I have encountered is that the voltage at the Vin pin of the Arduino turns into a square wave when firing, the closest I have come to filtering is was with use of a 7805 regulator but that causes different issues altogether.

« Last Edit: May 02, 2015, 09:23:43 PM by Zanderist »

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #30 on: May 03, 2015, 01:45:48 AM »
The input voltage will be pulsing as well since the battery is going from heavy load to light load over and over again.  I have a completely different idea that worked great! The only drawback is I can't continuously read resistance/current.

I went through the whole hardware/software filter thing. It takes way too long to get a nice flat signal and gives you bogus results either way.  Waste of time..... It may say 3v and read ok, but the actual heating effect is much higher than that. The lower you go with the duty cycle the worse it is.  RMS voltage seems spot on with the heating effect..   If you watch that video I linked, check the difference between RMS and mean voltage at low duty cycle.

On a side note, I've been watching all of pbusardo's videos where he's testing mods with a scope.  Not any of them get within the range that I got from the first test in that video.  It's pretty friggin' decent.  I hit it all the way up to 10v RMS with a 0.33 ohm build on it.  It was intesnse. :P 

EDIT:If you calculate it, 25% duty cycle will actually be 50% of RMS voltage(heating effect).  With 10v peak(loaded battery voltage) 25% duty cycle would give you 2.5v mean, when it's actually heating the coil(s) like 5v.  Make sense?  I don't know how else to explain it. Hope you get it. :)
« Last Edit: May 03, 2015, 01:51:10 AM by norcalreballer »

Offline Ian444

  • Jr. Member
  • **
  • Joined: Sep 2014
  • Location: QLD, Australia
  • Posts: 23
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #31 on: May 03, 2015, 07:47:40 AM »
Did you try timing the adc sample so it happens during one of the pulses, then convert to rms knowing the pulse width? I'm doing a project
at the moment that uses sampled voltage on the fly to calculate the pulse width, but I don't have a display so that is one less thing to
worry about. I'm using an Atmel ATtiny13, same family I think as the Arduino, but with only 5 in/outs and 1K of flash memory.

Sheesh 10V rms and a 0.33 ohm coil, are we talking 300W here? I'm way down at the exact opposite end of the scale, around 10W with a
single battery  :laughing:

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #32 on: May 03, 2015, 09:03:17 AM »
You can always convert mean to RMS from the duty cycle.

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #33 on: May 03, 2015, 01:37:51 PM »
I'm honestly new to electronics, hence trying to over explain stuff...  My first go around with the PWM mod was filtering with hardware, software and a combination of both.  I posted about it here.  It also shows how I'm doing it now.  Not the best solution, but it's easy to do and it works.   In short, I'm firing the atty at 100% like an unregulated mod for just long enough to get current and loaded battery voltage.  After that I calculate everything I need and pulse the atty.  I'm going to get it going faster now, but even 35ms isn't too long.

http://breaktru.com/smf/index.php/topic,1474.0.html

There is one guy I know that's basically turning the arduino into a scope to sample the wave directly and calculating everything from that.  Pretty sweet idea.  IMHO filtering for the mean then calculating RMS from that will simply take too long.   It's easy to test for yourself, though.   

Yes, 300w.  I wanted to see what this thing could do.  It wasn't even maxed out. :)

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #34 on: May 03, 2015, 03:23:07 PM »
It's all useful stuff. I've not tried PWM yet as I'm currently working with a raptor, so I'll defer to your experience :).

I am slightly surprised that it takes as long as 35ms to settle down though. What batteries are you using?

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #35 on: May 03, 2015, 03:49:22 PM »
This was due to how I'm reading the current.  Zero filtering.  It'll be faster on the next one.  If you think that's bad, try filtering the output.  It's much worse than that, haha! Good luck on your project!

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #36 on: May 04, 2015, 04:00:02 AM »
That ends up giving you the mean,  though.

No, it's not mean.  You put a hook in the code so you sample only during the pulse at max sampling rate, than average.  When you have a good read on peak voltage and peak current you can accurately calculate everything in terms of RMS values with duty cycle known.  Otherwise for a hardware filter, you use a time constant low enough so that voltage settles adequately before taking a "single" sample toward the end of the pulse.  Either way should yield stable and accurate readings.  If not there's a timing issue somewhere.

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #37 on: May 04, 2015, 12:30:01 PM »
That makes sense.  :)

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #38 on: May 05, 2015, 05:29:09 AM »
Rather than switching the gate directly from the pwm pin (max 40mA), wouldn't it make more sense to use a gate driver? And what are the main things to look for in one?

Offline Mayor

  • Jr. Member
  • **
  • Joined: Apr 2015
  • Posts: 7
  • Karma: +0/-0
Re: A basic Arduino PWM build for beginners.
« Reply #39 on: May 05, 2015, 01:29:39 PM »
On all the MCU mods I've read about the output limit is dependent on the atomizer coil like an OKR or raptor mod. What is the best way to make it so the set power is the same no matter the atomizer resistance, like in a sigelei,etc box?

Way more power than you need in the first place, and just some fancy math to read the atomizer resistance and adjust settings around it? What am I missing here?

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #40 on: May 05, 2015, 01:38:22 PM »
You can lower the frequency and use a current limiting resistor.  My tests have been at 122hz and limiting the current to about 20ma.  I was going to go with a gate driver at first, but with such a low frequency it's not needed, IMO.  The MOSFET is fully saturating with the current limited to 20ma at that frequency, so it should be all good.  I saw someone suggest a PNP & NPN transistor array so the gate gets battery voltage VS 3.3 or 5v to reduce Rds(on). 

@Mayor  After getting proper readings you can regulate the output in software. 

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #41 on: May 05, 2015, 02:24:03 PM »
Did you do some fancy register manipulation to get the frequency that low?

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +243/-1
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #42 on: May 05, 2015, 05:29:38 PM »
Generally it's not a good idea to drive capacitive loads directly off an MCU I/O pin, it can lead to MCU problems like brownouts and spurious resets.  It can be resolved by adding a resistor inline as mentioned, but it also slows down the transition time of the MOSFET.  They are not very efficient during on/off transitions so the faster they are the better.  There is a big dependence on switching frequency, the lower it is the less heating occurs in the MOSFET.  It's not uncommon to hear of overheating issues with MOSFETs due to lack of switching speed for high frequency stuff like power supplies.

There's also gate charge.  That can make a big difference since the higher the gate charge, the greater the drive current and the slower the MOSFET turns on and off.  There can be a pretty wide range there, but typically when minimizing on-state resistance you're maximizing gate charge.  That's how it works with MOSFETs, the bigger the gate junction the lower the on-state resistance but that maximizes gate charge.

Personally I would use a gate driver, but putting a resistor inline is good way to drop the part count and it probably does not have a big impact for typical PWM frequencies under 100Hz.  Though if you want to run frequencies a lot higher than that a gate driver might be necessary.



Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #43 on: May 05, 2015, 05:44:07 PM »
Cheers Craig :).

This is the gate driver I'm looking at. It's small and cheap. http://www.farnell.com/datasheets/1383716.pdf

I've got VW running pretty well with my test setup. I've re-used loads of code which helps. At the moment I'm driving a 3034 direct from the PWM pin at 980Hz, just for initial testing. I'm not bothered about burning $3 arduino out at the moment, I've got loads. It makes a noise when it's firing so I'm either going to have to go up in frequency out of hearing range (32kHz) or down to 122Hz and it sounds like down is the way to go.

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #44 on: May 05, 2015, 06:36:04 PM »
Driver is the proper way for sure.  Luckily I haven't had an issue with this yet at such a low frequency.  I could probably even lower it more.  I just switched to a 3.3v setup.  I'm going to use that transistor array to get battery voltage to the gate to reduce drain to source resistance.  Going to be working with some high current. 

You can change the frequency in one line of code.  Everyone else I know uses that pwm frequency library with arduino. 

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #45 on: May 05, 2015, 06:56:52 PM »
Ah OK is that with the PWM library? I can't seem to get it to work in the 1.6.3 IDE.

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #46 on: May 05, 2015, 08:12:42 PM »

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +35/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #47 on: May 05, 2015, 08:47:36 PM »
That's the register manipulation I was talking about. You have to be careful with that as it can affect other things. I'll have a play.

Which PWM pin are you using?

Offline norcalreballer

  • Sr. Member
  • ****
  • Joined: Jul 2014
  • Location: USA
  • Posts: 63
  • Karma: +4/-0
  • Gender: Male
Re: A basic Arduino PWM build for beginners.
« Reply #48 on: May 05, 2015, 10:01:57 PM »
Sorry for shirt messages...  Two job status over here.  I'm using timer 2. Timer 0 will affect millis()  and delay()

Offline PoppaVic60

  • Full Member
  • ***
  • Joined: Aug 2012
  • Location: United States
  • Posts: 31
  • Karma: +5/-0
Re: A basic Arduino PWM build for beginners.
« Reply #49 on: May 05, 2015, 10:57:27 PM »
Nice to see some activity,regardless. Particularly mcu/arduino/avr and pwm.

Hopefully we'll see some schematics and code?

Breaktru Forum  |  eCigarette Forum  |  Modding  |  Topic: A basic Arduino PWM build for beginners.
 

gfxgfx
gfx gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!