You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • Circuit is simple. AA battery holder connected to ground on the negative side, and to 2 1W resistors on the positive side. Each resistor is connected to the drain of a beefy N-channel mosfet (controlled by Espruino - pins A1 and A0). Voltage is monitored before the resistors, and after each resistors, in order to measure current (so I can calculate current without knowing the resistance of the fet).

    Logs are kept on SD card for graphing in excel. The idea is to be able to generate a discharge curve under various conditions, to get an idea what kind of battery to buy for every-day household electronics.

    The idea of using analog write is that I could later expand it to try to keep the current constant.

    I also have some constant current sinks coming I may try with this.

    Untested, so far.

    //Analog inputs
    //C3: Battery positive voltage
    //C2: Voltage after R1
    //C1: Voltage after R2
    
    //A1: R1 switch
    //A0: R2 switch
    
    
    var fetstate=[0,0];
    var resistance=[4.7,15];
    var endV;
    var inter;
    var timeout;
    var Wsec=0.0;
    var Asec=0.0;
    var runtime=0;
    var logname="logtest.txt"
    var fs=require("fs")
    
    function getMeasure() {
    	var retobj={};
    	var vbat=analogRead(C3)*E.getAnalogVRef();
    	var cur=0.0;
    	var pwr=0.0;
    	if (fetstate[0] > 0) {
    		var tv=vbat-analogRead(C2)*E.getAnalogVRef();
    		tcur=(tv/resistance[0])
    		pwr+=tcur*vbat;
    		cur+=tcur;
    	} 
    	if (fetstate[1] > 0) 
    		var tv=vbat-analogRead(C1)*E.getAnalogVRef();
    		tcur=(tv/resistance[0])
    		pwr+=tcur*vbat;
    		cur+=tcur;
    		cur+=(tv/resistance[1])
    	}
    	retobj.cur=cur;
    	retobj.pwr=pwr;
    	retobj.vbat=vbat;
    	return retobj;
    }
    
    function upfet() {
    	analogWrite(A1,fetstate[0]);
    	analogWrite(A1,fetstate[1]);
    }
    
    function startTest(fetst,endV,maxtime) {
    	fetstate=fetst;
    	runtime=0;
    	Asec=0.0;
    	Wsec=0.0;
    	var timeout=setTimeout("if (inter) {clearInterval(inter);endTest();}",maxtime*1000);
    	var inter=setInterval(runTest,1000);
    }
    
    function runTest() {
    	runtime+=1;
    	var obj=getMeasure();
    	var logstr=runtime+","+obj.vbat+","+obj.cur+","+obj.pwr+","+fetstate[0]+","+fetstate[1]+"\n"
    	console.log(logstr);
    	fs.appendFile(logname,logstr);
    	Wsec+=obj.pwr;
    	Asec+=obj.cur;
    	if (obj.vbat < endV) {
    		clearInterval(inter);
    		clearTimeout(timeout);
    		console.log("minimum voltage reached")
    	}
    }
    
    function endTest() {
    	digitalWrite(A0,0);
    	digitalWrite(A1,0);
    	console.log("Power: "+Wsec/3.6+" mWh Current: "+Asec/3.6+" mAh")
    }
    
    
About

Avatar for DrAzzy @DrAzzy started