// ///////////////////////////////////////////////////////////////////// // // ARDUINO/Genuino EVERY Sketch for "DIODEMOD" // https://www.changpuak.ch/electronics/Arduino-Diodemod.php // // WRITTEN 08.02.2566 BY ALEXANDER SSE FRANK // 23.02.2023 ADDED BPX65! // // The Serial Parser is from here : // Easy_Diseqc V1.2, Monstein ETH Zurich, 20.06.2018 // // ///////////////////////////////////////////////////////////////////// #include #define AT24C01_ADR 0x50 ; // EEPROM // SERIAL String buffer ; char tempbuf[80] ; // keeps the command temporary until CRLF // ///////////////////////////////////////////////////////////////////// // DAC ROUTINES FOR VOLTAGE LED AND VOLTAGE PHOTODIODE / DIODE // ///////////////////////////////////////////////////////////////////// float DC_CURR = 0.0 ; // mA const float MaxCurrent = 50.0 ; // mA const float R_Sense = 100.0 ; // OHMS // For MCP4725A0 the address is 0x60 or 0x61 // For MCP4725A1 the address is 0x62 or 0x63 // For MCP4725A2 the address is 0x64 or 0x65 // For MCP4725A3 the address is 0x66 or 0x67 const byte MCP4725_LED_VOLT_ADR = 0x67 ; const byte MCP4725_PHOTODIODE_VOLT_ADR = 0x66 ; #include Adafruit_MCP4725 DACLED ; Adafruit_MCP4725 DACPHOTO ; // SUPPLY VOLTAGE const int SupplyVoltPin = A6 ; const float SupplyVoltRatio = 1024 * (3.0 / (3.0 + 47.0 )) ; float SupplyVolt = 0.0 ; // Volts // LED CURRENT const int LEDCurrentPin = A7 ; const float LEDCurrentResistor = 100.0 ; // Ohm const float LEDCurrentMax = 49.0 ; // mA float LEDCurrent = 0.0 ; // mA float VOLT1 = 0.0 ; // V const float VOLT1min = 0.0 ; // V const float VOLT1max = 10.0 ; // V const float POWER1min = 0.0 ; // mW const float POWER1max = 19.9 ; // mW float POWER1 = 0.0 ; // mW float TRUEPOWER1 = 0.0 ; // mW // PD CURRENT AND VOLTAGE const int PDCurrentPin = A0 ; float PDCurrent = 0.0 ; const float Range3TrueR = 1.0 ; const float Range2TrueR = 99.99999 ; const float Range1TrueR = 9999.999 ; const float VOLT2min = 0.0 ; // SETPOINT const float VOLT2max = 10.0 ; // SETPOINT float VOLT2 = 0.0 ; // SETPOINT void UpdateLEDCurrent() { LEDCurrent = 0.01 * (float)(map(analogRead(LEDCurrentPin),0,1023,0,5000)) ; } void SET_VOLT1(float Setpoint) { int RAW = 1 ; if(Setpoint < VOLT1min) Setpoint = VOLT1min ; if(Setpoint > VOLT1max) Setpoint = VOLT1max ; RAW = (int)(Setpoint * 4095.0 / VOLT1max) ; // 12-Bit DACLED.setVoltage(RAW, false) ; } void SET_VOLT2(float Setpoint) { int RAW = 1 ; if(Setpoint < VOLT2min) Setpoint = VOLT2min ; if(Setpoint > VOLT2max) Setpoint = VOLT2max ; RAW = (int)(Setpoint * 4095.0 / VOLT2max) ; // 12-Bit DACPHOTO.setVoltage(RAW, false) ; } void SET_POWER1(float SETPOINT) { TRUEPOWER1 = 0.0 ; float VOLTAGESENSE = 0.0 ; const int MAXLOOP = 999 ; int LOOP = 0 ; if(SETPOINT > POWER1max) SETPOINT = POWER1max ; if(SETPOINT < POWER1min) SETPOINT = POWER1min ; VOLT1 = 0.0 ; LEDCurrent = 0.0 ; do { if(LEDCurrent < LEDCurrentMax) VOLT1 += 0.005 ; SET_VOLT1(VOLT1) ; UpdateLEDCurrent() ; LOOP += 1 ; VOLTAGESENSE = LEDCurrent * LEDCurrentResistor * 0.001 ; TRUEPOWER1 = (VOLT1 - VOLTAGESENSE) * LEDCurrent ; } while((TRUEPOWER1 < SETPOINT) && (LOOP < MAXLOOP)) ; } // RANGE PHOTODIODE CURRENT MEASUREMENT const int Range1RelaisPin = 5 ; // 10 kR const int Range2RelaisPin = 4 ; // 100 R const int Range3RelaisPin = 3 ; // 1 R int RESISTOR_RANGE = 3 ; // MEASURE PHOTODIODE VOLTAGE >>> MAX 5 VOLTS !!! const int VoltPDRelaisPin = 2 ; const int MeasVoltPDPin = A1 ; float PDVoltage = 0.0 ; void UpdatePDCurrent() { long RAW = 0 ; int Navg = 1000 ; for(int i=0; i STOPVOLTAGE) CONTINUE = 1 ; // EMERGENCY BREAK :-) UpdatePDCurrent() ; if(PDCurrent > STOPCURRENT) CONTINUE = 2 ; // EMERGENCY BREAK :-) if(LOOP > MAXLOOP) CONTINUE = 3 ; // EMERGENCY BREAK :-) if(PDVoltage > (LastVoltage + DeltaVoltage)) { Serial.print(PDVoltage, 3) ; Serial.print(" , ") ; Serial.println(PDCurrent, 3) ; LastVoltage = PDVoltage ; } } while(CONTINUE < 1) ; SET_VOLT2(0.0) ; digitalWrite(VoltPDRelaisPin, LOW) ; // DISCONNECT VOLT TO ADC if(CONTINUE == 1) Serial.println("Voltage limit exceeded.") ; if(CONTINUE == 2) Serial.println("Current limit exceeded.") ; if(CONTINUE == 3) Serial.println("Loop limit exceeded.") ; } void BPX65() { // SHORT PORT2 RESISTOR_RANGE = 3 ; // 1 Ω, 50 mA SET_RESISTOR_RANGE() ; delay(999) ; // SET PORT2 Resistor RESISTOR_RANGE = 1 ; // 10 kΩ, 5 µA SET_RESISTOR_RANGE() ; // DISCONNECT PORT2 VOLTAGE MEASUREMENT digitalWrite(VoltPDRelaisPin, LOW) ; // INIT COMPLETE float POWER1 = 0.0 ; float BIAS2 = 0.0 ; const float REVERSEmin = 0.0 ; const float REVERSEdelta = 0.5 ; const float POWER1min = 0.0 ; const float POWER1delta = 2.0 ; const int LOOP = 11 ; // POWER LED1 for(int i=0; i<=LOOP; i++) { // REVERSE BIAS VOLT2 for(int j=0; j<=LOOP; j++) { if((i==0)&&(j==0)) { // HEADER POS 0,0 // Serial.print(" ") ; // Serial.print(" µA ") ; Serial.print(" nA ") ; } if((i==0)&&(j>0)) { // REVERSE BIAS VOLTAGE BIAS2 = -1.0 * (5.5 - (REVERSEmin + (float)j * REVERSEdelta)) ; Serial.print(BIAS2, 2) ; // SET_VOLT2(abs(BIAS2)) ; } if((i>0)&&(j==0)) { POWER1 = POWER1min + (float)(i-1) * POWER1delta ; Serial.print(POWER1, 2) ; // SET_POWER1(POWER1) ; } // ----- MAIN LOOP ----- if((i>0)&&(j>0)) { // line := i POWER1 = POWER1min + (float)(i-1) * POWER1delta ; SET_POWER1(POWER1) ; // column = j BIAS2 = +1.0 * (5.5 - (REVERSEmin + (float)j * REVERSEdelta)) ; SET_VOLT2(BIAS2) ; delay(999) ; // element i,j UpdatePDCurrent() ; Serial.print(1000*PDCurrent, 1) ; } // ----- MAIN LOOP ----- if((i<=LOOP) && (j<=(LOOP-1))) Serial.print(",\t") ; } // EOL 555 :-) Serial.println(" ") ; } Serial.println("finished.") ; SET_POWER1(0.0) ; } // ///////////////////////////////////////////////////////////////////// // S E T U P // ///////////////////////////////////////////////////////////////////// void setup() { Serial.begin(115200) ; Wire.begin() ; // ADC analogReference(EXTERNAL) ; // REF02 CONNECTED HERE pinMode(SupplyVoltPin, INPUT) ; pinMode(MeasVoltPDPin, INPUT) ; pinMode(LEDCurrentPin, INPUT) ; pinMode(Range1RelaisPin, OUTPUT) ; pinMode(Range2RelaisPin, OUTPUT) ; pinMode(Range3RelaisPin, OUTPUT) ; pinMode(VoltPDRelaisPin, OUTPUT) ; RESISTOR_RANGE = 0 ; SET_RESISTOR_RANGE() ; digitalWrite(VoltPDRelaisPin, LOW) ; DACLED.begin(MCP4725_LED_VOLT_ADR) ; DACPHOTO.begin(MCP4725_PHOTODIODE_VOLT_ADR) ; SET_VOLT1(0.0) ; SET_VOLT2(0.0) ; } // ///////////////////////////////////////////////////////////////////// // M A I N // ///////////////////////////////////////////////////////////////////// void loop() { while (Serial.available() > 0) // ////////////////////////////////// { int tmp; char st[20]; char rx = Serial.read(); // read a single character buffer += rx; //add character to the string buffer if ((rx == '\n') || (rx == '\r')) { buffer.toCharArray(tempbuf, 40); // ///////////////////////////////////////////////////////// if (buffer.startsWith("SETRNG:")) // ///////////////////////////////////////////////////////// { sscanf(tempbuf,"SETRNG:%s",&st); // extract Range as float RESISTOR_RANGE = (int)strtod(st,NULL) ; SET_RESISTOR_RANGE() ; Serial.print("Range set to ") ; Serial.print(RESISTOR_RANGE, DEC) ; if(RESISTOR_RANGE == 0) Serial.println(" all off.") ; if(RESISTOR_RANGE == 1) Serial.println(" 10 kΩ") ; if(RESISTOR_RANGE == 2) Serial.println(" 100 Ω") ; if(RESISTOR_RANGE == 3) Serial.println(" 1 Ω") ; if(RESISTOR_RANGE == 4) Serial.println(" all on.") ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("SETV1:")) // ///////////////////////////////////////////////////////// { sscanf(tempbuf,"SETV1:%s",&st); // extract Voltage as float float SETPOINT = strtod(st,NULL) ; if(SETPOINT > VOLT1max) SETPOINT = VOLT1max ; if(SETPOINT < VOLT1min) SETPOINT = VOLT1min ; SET_VOLT1(SETPOINT) ; Serial.print("LED Voltage set to ") ; Serial.print(SETPOINT, 3) ; Serial.println(" V.") ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("SETP1:")) // ///////////////////////////////////////////////////////// { sscanf(tempbuf,"SETP1:%s",&st); // extract Power as float float SETPOINT = 0.0 ; SETPOINT = strtod(st,NULL) ; SET_POWER1(SETPOINT) ; UpdateLEDCurrent() ; Serial.print("LED Power @ PORT 1 set to ") ; Serial.print(TRUEPOWER1, 3) ; Serial.println(" mW.") ; Serial.print(VOLT1, 3) ; Serial.print(" V, ") ; Serial.print(LEDCurrent, 3) ; Serial.println(" mA. ") ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("SETV2:")) // ///////////////////////////////////////////////////////// { sscanf(tempbuf,"SETV2:%s",&st); // extract Voltage as float float SETPOINT = 0.0 ; SETPOINT = strtod(st,NULL) ; SET_VOLT2(SETPOINT) ; Serial.print("PD Voltage set to ") ; Serial.print(SETPOINT, 3) ; Serial.println(" V.") ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("ILED?")) // ///////////////////////////////////////////////////////// { UpdateLEDCurrent() ; Serial.print("LED Current is ") ; Serial.print(LEDCurrent, 3) ; Serial.println(" mA.") ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("IPD?")) // ///////////////////////////////////////////////////////// { UpdatePDCurrent() ; Serial.print("PD Current is ") ; Serial.print(PDCurrent, 3) ; if(RESISTOR_RANGE == 3) Serial.println(" mA.") ; if(RESISTOR_RANGE == 2) Serial.println(" µA.") ; if(RESISTOR_RANGE == 1) Serial.println(" µA.") ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("*IDN?")) // ///////////////////////////////////////////////////////// { GREETINGS() ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("SUPPLY?")) // ///////////////////////////////////////////////////////// { Update_Supply() ; Serial.println(SupplyVolt,3) ; } // ///////////////////////////////////////////////////////// if (buffer.startsWith("BY228!")) // ///////////////////////////////////////////////////////// { RESISTOR_RANGE = 3 ; SET_RESISTOR_RANGE() ; Serial.println("DIODE TEST : BY228") ; BY228() ; } else // ///////////////////////////////////////////////////////// if (buffer.startsWith("BPX65!")) // ///////////////////////////////////////////////////////// { Serial.println("PHOTODIODE TEST : BPX65") ; BPX65() ; } else // ///////////////////////////////////////////////////////// buffer = "" ; //erase buffer for next command // ///////////////////////////////////////////////////////// } } delay(9) ; } // ///////////////////////////////////////////////////////////////////// // // END OF FILE // // /////////////////////////////////////////////////////////////////////