Hi guys
i have designed a dummy load based on DAVE Jones, MJlorton and Jjoster Dummyload and have used libraries from JJoster for the rotary encoder and Libraries from ADAfruit for DAC and the INA 219.
MCP 4725 is the DAC to set the voltage ( 0 - 4096 steps which corresponds to 0 - 5000 mV ) on the LM324 to control the mosfet and finally i am using arduino 328 to control the DAC and INA 219 ( current , voltage, power reading @ 12bit) and an IIC lcd to display the settings and readings of a rotary encoder to set the current.
am still writing program and its not that perfect
else am not an expert in programming n electronics am still learning n most of time i use libraries made by some experts, like D jones, JJoster,Mjlorton n ...
tonight or tomorrow am going to post a video of it
Description of the LCD
BV means Bus Voltage in volts
mA means current flowing through the resistor acting as load
The Number 2 ( range 0,1,2,3) stand for Multiplier that it is it multiplies the rotary encoder values
The code Running
[/code
//version 1
#include <RotaryEncoder.h>
// A0-A1-A2 are grounded so I2C Address is 0x38
/*-----( Import needed libraries )-----*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#include <Adafruit_INA219.h>
#include <Adafruit_MCP4725.h>
Adafruit_INA219 ina219;
Adafruit_MCP4725 dac;
unsigned int constant_millis = 500; //TC77 delay between readings
unsigned long old_millis = 0;
// rotatary variables
RotaryEncoder encoder(A0,A2,5,6,3000);
//(int ENC_A, int ENC_B, int multiplier, int stepSize, int pauseLength)
int val = 0;
int cnt = 0;
char s[21];
int remainder;
#define debounce 20 // ms debounce period to prevent flickering when pressing or releasing the button
#define holdTime 700 // ms hold period: how long to wait for press+hold event
int buttonVal = 0; // value read from button
long btnDnTime; // time the button was pressed down
//Delay
int Dcount = 0;
//Download: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move original LiquidCrystal library elsewhere, copy this in it's place
/*-----( Declare Constants )-----*/
#define I2C_ADDR 0x20 // Define I2C Address for the PCF8574T
//---(Following are the PCF8574 pin assignments to LCD connections )----
// This are different than earlier/different I2C LCD displays
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define LED_OFF 1
#define LED_ON 0
int test = 0;
//float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
// float loadvoltage = 0;
//float power = 0;
/*-----( Declare objects )-----*/
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() /*----( SETUP: RUNS ONCE )----*/
{
//Serial.begin(57600);
lcd.begin (16,2); // initialize the lcd
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(LED_ON);
lcd.clear();
lcd.home();
// Print our characters on the LCD
lcd.backlight(); //Backlight ON if under program control
delay(500);
ina219.begin();
delay(500);
dac.begin(0x62);
pinMode(2, INPUT_PULLUP);
}// END Setup
void WriteLCD1(void)
{
lcd.setCursor(0,0);
lcd.print(busvoltage);
lcd.setCursor(5,0);
lcd.print("BV");
lcd.setCursor(8,0);
lcd.print(current_mA);
lcd.setCursor(14,0);
lcd.print("mA");
//DAC
lcd.setCursor(7,1);
sprintf(s,"%2d",val/1);
lcd.print(s);
lcd.print('.');
lcd.setCursor(12,1);
lcd.print(" DAC");
// lcd.setCursor(0,1);
// lcd.print(loadvoltage);
//lcd.setCursor(4,1);
// lcd.print("LDV");
lcd.setCursor(0,1);
lcd.print(cnt);
//lcd.setCursor(1,1);
//lcd.print(power);
}
void Updatedac(void)
{
uint32_t counter;
// Run through the full 12-bit scale for a triangle wave
counter =val;
dac.setVoltage(counter, false);
}
void CurrentSense(void)
{
//shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
//loadvoltage = busvoltage + (shuntvoltage / 1000);
// power= busvoltage*current_mA;
// Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
// Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
//Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
//Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
//Serial.println("");
}
void loop() /*----( LOOP: RUNS OVER AND OVER AGAIN )----*/
{
int enc = encoder.readEncoder();
int changevalue = 1;
if(enc != 0) {
val = val + (enc);
val = min(val,4095);
val = max(val,0);
if ((cnt == 1) && ( enc == 1)) {
val = val + 1;
}
if ((cnt == 2) && ( enc == 1)) {
val = val + 10;
}
if ((cnt == 3) && ( enc == 1)) {
val = val + 100;
}
//reverse
if ((cnt == 1) && ( enc <= 0)) {
val = val - 1;
}
if ((cnt == 2) && ( enc <= 0)) {
val = val - 10;
}
if ((cnt == 3) && ( enc <= 0)) {
val = val - 100;
}
if (val >= 4096) {
val = 4095;
}
if (val <= 0) {
val = 0;
}
Updatedac();
}
Dcount++;
int buttonVal = digitalRead(2);
if ((millis() - old_millis) > constant_millis) {
WriteLCD1();
old_millis = millis();
}
if (Dcount>4000)
{
CurrentSense();
Dcount=0; //reset the counter
if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime))
{
btnDnTime = millis();
}
}
if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime))
{
cnt++;
btnDnTime = millis();
if (cnt >= 4) {
cnt = 0;
}
}
} // END Loop
]