Using PortD & PortB to set input bits for DAC using Sparkfun Redboard v3


int dataWord;

//DataBus Pin Declaration---I would like to get rid of this and only use PORTD/PORTB manipulation.
int DB0 = 2; //Digital Pin 2 on Sparkfun
int DB1 = 3;
int DB2 = 4;
int DB3 = 5;
int DB4 = 6;
int DB5 = 7;
int DB6 = 8;
int DB7 = 9; //Digital Pin 9 on Redboard

//Voltage Declaration
float voltage1;
float voltage2;
float voltage3;

//Power Declaration
float power1;

//RelayPin Digital Pin Declaration
int relayPin = 11;
const int timeDelay = 500;

//Write/Pulse Declaration
int WR = 13;

String state = "";

void setup()
{

  Serial.begin(9600);
  while(!Serial);


  pinMode(DB0,OUTPUT); //set Pin 0 as an output
  pinMode(DB1,OUTPUT); //set Pin 1 as an output
  pinMode(DB2,OUTPUT); //set Pin 2 as an output
  pinMode(DB3,OUTPUT); //set Pin 3 as an output
  pinMode(DB4,OUTPUT); //set Pin 4 as an output
  pinMode(DB5,OUTPUT); //set Pin 5 as an output
  pinMode(DB6,OUTPUT); //set Pin 6 as an output
  pinMode(DB7,OUTPUT); //set Pin 7 as an output
  

  //Select Line A2 set to Ground due to lack of Pins. 
  pinMode(A0, INPUT); //Analog In Voltage
  pinMode(A2, INPUT); //Analog In Voltage
  pinMode(A4, INPUT); //Analog In Voltage
  
  //Bits 0-7---
  digitalWrite(DB0,HIGH); //These are the values I would like to change from the Serial Monitor
  digitalWrite(DB1,LOW); //Right now I manually change these from Low to High
  digitalWrite(DB2,HIGH);
  digitalWrite(DB3,LOW);
  digitalWrite(DB4,HIGH);
  digitalWrite(DB5,HIGH);
  digitalWrite(DB6,HIGH);
  digitalWrite(DB7,HIGH);
  

  //Write line setup
  pinMode(WR, OUTPUT);
  pinMode(WR, OUTPUT);
  pinMode(WR, OUTPUT);

  //Write line pulse
  digitalWrite(WR, HIGH);
  digitalWrite(WR, LOW);
  digitalWrite(WR, HIGH);

  //Setup for relay(relayPin) and the analog output(A5) of relay circuit
  pinMode(relayPin, OUTPUT);
  pinMode(A5, INPUT);

  //Turns the system on to begin testing
  digitalWrite(relayPin, HIGH);  // turn the relay on
  delay(timeDelay);              // wait for one second

}


void portSet(byte x){

  byte a = x;
  byte y;
  byte z;
  // SET PORTD 2-7 AS OUTPUT
  DDRD = DDRD | B11111100;
  // SET PORTB 8-9 AS OUTPUT
  DDRB = DDRB | B00000011;

  x = ~x;
  y = x & B11000000;
  y = y >> 6;
  PORTB = PORTB | y;
  z = x << 2;
  z = z & B11111100;
  PORTD = PORTD | z;

}

void loop(){



  //Begin serial comms
  if(Serial.available() == 0){

    state = Serial.readString();
    
     //Loop to manually turn the circuit off
    if (state == "p off") {
      Serial.print("");
      digitalWrite(relayPin, LOW);
      Serial.println("SYSTEM IS OFF!");
    }

    //Loop to manually turn the circuit on
    if (state == "p on") {
      Serial.print("");
      digitalWrite(relayPin, HIGH);
      Serial.println("SYSTEM IS ON!");
    }

    //Loop to manually print voltage1
    if (state == "voltage1") {
      Serial.print("");
      Serial.print("Voltage1 is: ");
      Serial.print(voltage1,4);
      Serial.println(" V");

    }

    //Loop to manually print power1
    if (state == "power1"){
      Serial.print("Power1 is: ");
      Serial.print(power1,5);
      Serial.println(" W");
      delay(2000);
    }

    //Loop to manually print voltage2
    if (state == "voltage2") {
      Serial.print("");
      Serial.print("Voltage2 is: ");
      Serial.print(voltage2,4);
      Serial.println(" V");
    }
    //Loop to manually print voltage3
    if (state == "voltage3") {
      Serial.print("");
      Serial.print("Voltage3 is: ");
      Serial.print(voltage3,4);
      Serial.println(" V");
  }
  }

  if((dataWord >= 0) && (dataWord <= 255)){
    dataWord = Serial.parseInt();
    Serial.println(dataWord, BIN);
    }
  
    //If/Print statement if voltage is out of lower bound
    if(voltage1 < voltage2){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too low!");
     }
    //If/Print statement if voltage is out of higher bound
    if(voltage1 > voltage3){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too high!");
    }
    //If/Print statement if voltage is in correct range
    if ((voltage1 >= voltage2) && (voltage1 <= voltage3)){
    digitalWrite(relayPin, HIGH);
    }  

}

This may be long but I wanted to be thorough but the overall idea of this project is that I have a Sparkfun Redboard v3, hooked up to an AD7228 DAC chip(I'll link the datasheet) and a transistor/relay circuit(I am attaching a schematic). Essentially, I am able to edit the 8 data bits from the DAC by setting them high or low to change the output voltage of the DAC. That output voltage is then read in by my Redboard through an Analog PIN, and if that voltage is within my voltage range that I set, my circuit will turn on and run continuously unless I set a voltage that is too high or too low. In that case, the circuit will print a warning and turn off. Currently, the circuit works in a manual fashion, I can go into my code in the Arduino IDE and manually change bits 0-7 from the DAC(2-9 on the Arduino as I saw that using PINS 0 & 1 is not advisable) to HIGH or LOW to get different voltage outputs. I would like to be able to set these values from the Serial Monitor so that I can set them all at the same time but I have not had much luck as I'm using both PORTD & PORTB.

In summary, I would like to be at the Serial Monitor, enter in a 8 bit word and it will set the values for my 8 PINS and adjust my output voltage on the DAC. In the screenshot of my schematic, please ignore the model # as Fritzing did not have the part I needed but the functionality is the same. I apologize if I didn't explain something well, I tried to stick close to the posting guidelines but thank you in advance for your help.

AD7228A DAC

Welcome to the forum. Start by reading this tutorial: Serial Input Basics - updated

You do not need to use direct port manipulation. your DAC doesn't care what state the pins are in until the WR pin goes LOW to HIGH. While WR is HIGH, your DAC doesn't change.

When setting a new value,
WR is HIGH
set A0, A1, A2
set each of your DB pins according to your input value
Pulse WR LOW, then back to HIGH and the new value will appear.

You could put your pins in an array on use a for() loop to set them all. Also look at the bitRead() function to test each bit in your input for 0 or 1

Something like this will get your started...

int dataWord;

#if 0
//DataBus Pin Declaration---I would like to get rid of this and only use PORTD/PORTB manipulation.
int DB0 = 2; //Digital Pin 2 on Sparkfun
int DB1 = 3;
int DB2 = 4;
int DB3 = 5;
int DB4 = 6;
int DB5 = 7;
int DB6 = 8;
int DB7 = 9; //Digital Pin 9 on Redboard
#endif

// pins, LSB to MSB
const int pinCount = 8; // 8 data bits
const byte adcOutputPin[pinCount] = { 2,3,4,5,6,7,8,9 };

//Write/Pulse Declaration
const byte adcWRPin = 13;

//Voltage Declaration
float voltage1;
float voltage2;
float voltage3;

//Power Declaration
float power1;

//RelayPin Digital Pin Declaration
const int relayPin = 11;
const unsigned long timeDelay = 500;


String state = "";

void setup()
{

  Serial.begin(9600);
  while (!Serial);

  for( int i = 0; i < pinCount; ++i ) {
    pinMode(adcOutputPin[i], OUTPUT );
  }
  pinMode(adcWRPin, OUTPUT);
  digitalWrite(adcWRPin, HIGH );

  setAdc( 0b11110101 );

#if 0
  //Bits 0-7---
  digitalWrite(DB0, HIGH); //These are the values I would like to change from the Serial Monitor
  digitalWrite(DB1, LOW); //Right now I manually change these from Low to High
  digitalWrite(DB2, HIGH);
  digitalWrite(DB3, LOW);
  digitalWrite(DB4, HIGH);
  digitalWrite(DB5, HIGH);
  digitalWrite(DB6, HIGH);
  digitalWrite(DB7, HIGH);
#endif

  //Setup for relay(relayPin) and the analog output(A5) of relay circuit
  pinMode(relayPin, OUTPUT);

  //Turns the system on to begin testing
  digitalWrite(relayPin, HIGH);  // turn the relay on
  delay(timeDelay);              // wait for one second
}


void setAdc(byte x) {

  for ( int i=0; i < pinCount; ++i ) {
    if ( bitRead( x, i ) == 1 ) {
      digitalWrite( adcOutputPin[i], HIGH );
    }
    else {
      digitalWrite( adcOutputPin[i], LOW );
    }
  }

  // latch new value
  digitalWrite( adcWRPin, LOW );
  digitalWrite( adcWRPin, HIGH );
}

I have no idea what your loop() is doing since you never assign any value to voltage1, voltage2, voltage3, power1

Thank you for linking the tutorial, I'll step through that to get a better understanding because I thought I needed to use port manipulation. Also, the loop is just where I am printing my voltages, voltage1 & power1 are the output voltage and power from my DAC. Voltage2 & 3 are the voltage limits I have set using single-turn potentiometers. I'm just comparing my output voltage to the limits determine whether or not to shut the circuit off.

Thank you for the reply and I'll get working on the snippet you attached!

-J

But you never set these values, so they will always be zero. If you want to set them, you will need to use analogRead() to get the values before you use them.

Also, it is much better to use variables names that reflect what the variable represents. Which is more clear?

float voltage1, voltage2, voltage3;

or

float dacVoltage, lowerVoltageLimit, upperVoltageLimit;

Variable names are for humans to help them understand.

As a side note, it seems very complicated to have a relay run by a transistor for the sole purpose of driving either a green or red LED. You could just wire them up directly to the Uno through a resistor and eliminate all of that. A1 and A3 are available. You can use the analog pins as digital pins, by the way. They are not only for analog.

Oh, sorry I realized I left out a section of the code, I'm reattaching the snippet.

 // Power Supply Monitor
int VOLTS1 = A0; //initialize A1 as the pin for voltage
int VOLTS2 = A2; //set pin for ADC2, reading in Min Voltage
int VOLTS3 = A4; //set pin for ADC3, reading in Max Voltage

int dataWord;

//DataBus Pin Declaration
int DB0 = 2;
int DB1 = 3;
int DB2 = 4;
int DB3 = 5;
int DB4 = 6;
int DB5 = 7;
int DB6 = 8;
int DB7 = 9;

//Voltage Declaration
float voltage1;
float voltage2;
float voltage3;

//Power Declaration
float power1;

//RelayPin Digital Pin Declaration
int relayPin = 11;
const int timeDelay = 500;

//Write/Pulse Declaration
int WR = 13;

String state = "";

void setup()
{

  Serial.begin(9600);
  while(!Serial);


  pinMode(DB0,OUTPUT); //set Pin 0 as an output
  pinMode(DB1,OUTPUT); //set Pin 1 as an output
  pinMode(DB2,OUTPUT); //set Pin 2 as an output
  pinMode(DB3,OUTPUT); //set Pin 3 as an output
  pinMode(DB4,OUTPUT); //set Pin 4 as an output
  pinMode(DB5,OUTPUT); //set Pin 5 as an output
  pinMode(DB6,OUTPUT); //set Pin 6 as an output
  pinMode(DB7,OUTPUT); //set Pin 7 as an output
  

  //Select Line A2 set to Ground due to lack of Pins. 
  pinMode(A0, INPUT); //Analog In Voltage
  pinMode(A2, INPUT); //Analog In Voltage
  pinMode(A4, INPUT); //Analog In Voltage
  
  //Bits 0-7---Would like to replace this with serial monitor input.
  digitalWrite(DB0,HIGH);
  digitalWrite(DB1,LOW);
  digitalWrite(DB2,HIGH);
  digitalWrite(DB3,LOW);
  digitalWrite(DB4,HIGH);
  digitalWrite(DB5,HIGH);
  digitalWrite(DB6,HIGH);
  digitalWrite(DB7,HIGH);
  

  //Write line setup
  pinMode(WR, OUTPUT);
  pinMode(WR, OUTPUT);
  pinMode(WR, OUTPUT);

  //Write line pulse
  digitalWrite(WR, HIGH);
  digitalWrite(WR, LOW);
  digitalWrite(WR, HIGH);

  //Setup for relay(relayPin) and the analog output(A5) of relay circuit
  pinMode(relayPin, OUTPUT);
  pinMode(A5, INPUT);

  //Turns the system on to begin testing
  digitalWrite(relayPin, HIGH);  // turn the relay on
  delay(timeDelay);              // wait for one second

}


void portSet(byte x){

  byte a = x;
  byte y;
  byte z;
  // SET PORTD 2-7 AS OUTPUT
  DDRD = DDRD | B11111100;
  // SET PORTB 8-9 AS OUTPUT
  DDRB = DDRB | B00000011;

  x = ~x;
  y = x & B11000000;
  y = y >> 6;
  PORTB = PORTB | y;
  z = x << 2;
  z = z & B11111100;
  PORTD = PORTD | z;

}

void loop(){

//I forgot to put this back in the code tags when copying everything over. 
  //Voltage and power calculations for DAC1
  //DAC1
  int sensorValue = analogRead(VOLTS1); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float voltage1 = (sensorValue * (5.0) /1023.0); //initialize voltage as a float
  float power1 = ((voltage1 * voltage1)/10000);

  //DAC2
  int sensorValue2 = analogRead(VOLTS2); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float voltage2 = (sensorValue2 * (5.0) /1023.0); //initialize voltage as a float

  //DAC3
  int sensorValue3 = analogRead(VOLTS3); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float voltage3 = (sensorValue3 * (5.0) /1023.0); //initialize voltage as a float


  //Begin serial comms
  if(Serial.available() == 0){

    state = Serial.readString();
    
    //Loop to manually turn the circuit off
    if (state == "p off") {
      Serial.print("");
      digitalWrite(relayPin, LOW);
      Serial.println("SYSTEM IS OFF!");
    }

    //Loop to manually turn the circuit on
    if (state == "p on") {
      Serial.print("");
      digitalWrite(relayPin, HIGH);
      Serial.println("SYSTEM IS ON!");
    }

    //Loop to manually print voltage1
    if (state == "voltage1") {
      Serial.print("");
      Serial.print("Voltage1 is: ");
      Serial.print(voltage1,4);
      Serial.println(" V");

    }

    //Loop to manually print power1
    if (state == "power1"){
      Serial.print("Power1 is: ");
      Serial.print(power1,5);
      Serial.println(" W");
      delay(2000);
    }

    //Loop to manually print voltage2
    if (state == "voltage2") {
      Serial.print("");
      Serial.print("Voltage2 is: ");
      Serial.print(voltage2,4);
      Serial.println(" V");
    }
    //Loop to manually print voltage3
    if (state == "voltage3") {
      Serial.print("");
      Serial.print("Voltage3 is: ");
      Serial.print(voltage3,4);
      Serial.println(" V");
    }
  }


  if((dataWord >= 0) && (dataWord <= 255)){
    dataWord = Serial.parseInt();
    Serial.println(dataWord, BIN);
    }
  
    //If/Print statement if voltage is out of lower bound
    if(voltage1 < voltage2){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too low!");
     }
    //If/Print statement if voltage is out of higher bound
    if(voltage1 > voltage3){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too high!");
    }
    //If/Print statement if voltage is in correct range
    if ((voltage1 >= voltage2) && (voltage1 <= voltage3)){
    digitalWrite(relayPin, HIGH);
    }  

    Serial.println(voltage1);

}

I'll make a note to change the variable names to make them easier to read, thank you for the tip. As far as the circuit design, I'll look into simplifying it, I just combined knowledge from a few example projects to incorporate different components but more isn't always better I guess. Now I just need to tackle the possibility of inputting the DAC values from the SM rather than inside the code.

Thank you again for your responses!

snippets are never useful. Always attach a complete sketch that someone can copy & paste into their IDE to test/help.

1 Like
//EENG 497/498 Redboard Power Supply Monitor
int VOLTS1 = A0; //initialize A1 as the pin for voltage
int VOLTS2 = A2; //set pin for ADC2, reading in Min Voltage
int VOLTS3 = A4; //set pin for ADC3, reading in Max Voltage


// pins, LSB to MSB
const int pinCount = 8; // 8 data bits
const byte dacOutputPin[pinCount] = { 2,3,4,5,6,7,8,9 };

//Write/Pulse Declaration
const byte dacWRPin = 13;

//Voltage Declaration
float dacVoltage;
float lowerVoltageLimit;
float upperVoltageLimit;
float setVoltage;

//Power Declaration
float power;

//RelayPin Digital Pin Declaration
const int relayPin = 11;
const unsigned long timeDelay = 500;

//State for SM Input
String state = "";

void setup()
{

  Serial.begin(9600);
  while (!Serial);

  //Setting up Pin Declaration for DB Pins & Write Pin
  for( int i = 0; i < pinCount; ++i ) {
    pinMode(dacOutputPin[i], OUTPUT );
  }
  pinMode(dacWRPin, OUTPUT);
  digitalWrite(dacWRPin, HIGH );

  setDac( 0b11110001 );

  //Setup for relay(relayPin) and the analog output(A5) of relay circuit
  pinMode(relayPin, OUTPUT);

  //Turns the system on to begin testing
  digitalWrite(relayPin, HIGH);  // turn the relay on
  delay(timeDelay);              // wait for one second
}

//Method for DAC Output
void setDac(byte x) {

  for ( int i=0; i < pinCount; ++i ) {
    if ( bitRead( x, i ) == 1 ) {
      digitalWrite( dacOutputPin[i], HIGH );
    }
    else {
      digitalWrite( dacOutputPin[i], LOW );
    }
  }

  // latch new value
  digitalWrite( dacWRPin, LOW );
  digitalWrite( dacWRPin, HIGH );

  Serial.println("Hello Justin, Want to Play A Game?");
}

void loop(){


  //Voltage and power calculations for DAC1
  //DAC1
  int sensorValue = analogRead(VOLTS1); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float dacVoltage = sensorValue * (5.0 / 1023.0); //initialize voltage as a float
  float power = ((dacVoltage * dacVoltage)/10000);

  //Lower Voltage Limit Calculations
  int sensorValue2 = analogRead(VOLTS2); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float lowerVoltageLimit = (sensorValue2 * (5.0) /1023.0); //initialize voltage as a float
  
  
  //Upper Voltage Limit Calculations
  int sensorValue3 = analogRead(VOLTS3); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float upperVoltageLimit = (sensorValue3 * (5.0) /1023.0); //initialize voltage as a float

  //float lowerVoltageLimit = 0.8;
  //float upperVoltageLimit = 3.3;


  //Test Commands
  if(Serial.available() == 0){

    state = Serial.readString();
    
    //Statement to manually turn the circuit off
    if (state == "SYSOFF") {
      Serial.print("");
      digitalWrite(relayPin, LOW);
      setDac( 0b00000000 );
      Serial.println("SYSTEM IS OFF!");
    }
    //Statement to manually turn the circuit on
    if (state == "SYSON") {
      Serial.print("");
      digitalWrite(relayPin, HIGH);
      setDac( 0b10000001 );
      Serial.println("SYSTEM IS ON!");
    }
    //Statement to manually print dacVoltage
    if (state == "VDAC") {
      Serial.print("");
      Serial.print("DAC Voltage is: ");
      Serial.print(dacVoltage,4);
      Serial.println(" V");
    }
    //Statement to manually print power
    if (state == "PWR"){
      Serial.print("Power is: ");
      Serial.print(power,5);
      Serial.println(" W");
    }
    //Statement to manually print lowerVoltageLimit
    if (state == "LV") {
      Serial.print("");
      Serial.print("Lower Voltage Bound is set to: ");
      Serial.print(lowerVoltageLimit,4);
      Serial.println(" V");
    }
    //Statement to manually print upperVoltageLimit
    if (state == "HV") {
      Serial.print("");
      Serial.print("Upper Voltage Bound is set to: ");
      Serial.print(upperVoltageLimit,4);
      Serial.println(" V");
    }
    /*Statement to Adjust DAC Value
    if (state == "CHDAC") {
      Serial.print("");
      Serial.print("Please enter a new byte: ");
    }
    */
  }
    //If/Print statement if voltage is out of lower bound
    if(dacVoltage < lowerVoltageLimit){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too low!");
     }
    //If/Print statement if voltage is out of higher bound
    if(dacVoltage > upperVoltageLimit){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too high!");
    }
    //If/Print statement if voltage is in correct range
    if ((dacVoltage >= lowerVoltageLimit) && (dacVoltage <= upperVoltageLimit)){
    digitalWrite(relayPin, HIGH);
    }  

    Serial.print("The voltage is: ");
    Serial.println(dacVoltage);
    delay(1000);
}

I have another question, @blh64 your code has helped me clean everything up and the system works as it should. My last question is there any way to input the byte for setDac from the Serial Monitor? As of right now I can hard code a new byte in the actual code but I want to know if there is a way to change that from the SM? So instead of changing setDac(0b--------) I can have the Serial Monitor prompt the user for a new byte, then they can enter in either a decimal value of 0-255 that's equivalent to the binary value or just the 8 bit binary string. For example, the program starts:

"Please enter in a new DAC set voltage:"
User enters: 233
Now, the DAC will read in the binary value of 233 which is 11101001 and set PINS2-9 to 11101001 respectively. I have read a lot of tutorials and maybe I am missing something but I'm just having trouble with this.

Thank you in advance.

J.

Read this: Serial Input Basics - updated

It explains it all.

//EENG 497/498 Redboard Power Supply Monitor
int VOLTS1 = A0; //initialize A0 as the pin for voltage


// pins, LSB to MSB
const int pinCount = 8; // 6 data bits
const byte dacOutputPin[pinCount] = { 9,8,7,6,5,4,3,2 };


//Write/Pulse Declaration
const byte dacWRPin = 13;

//Voltage Declaration
float dacVoltage;
float lowerVoltageLimit;
float upperVoltageLimit;


//Power Declaration
float power;

//RelayPin Digital Pin Declaration
const int relayPin = 11;
const unsigned long timeDelay = 500;

//State for SM Input
String state = "";

void setup()
{

  Serial.begin(9600);
  while (!Serial);

  pinMode(VOLTS1, INPUT);
  analogReference(DEFAULT);  // Set the ADC reference voltage to the default (power supply voltage)

  //Setting up Pin Declaration for DB Pins & Write Pin
  for( int i = 0; i < pinCount; ++i ) {
    pinMode(dacOutputPin[i], OUTPUT );
  }

  
  pinMode(dacWRPin, OUTPUT);
  digitalWrite(dacWRPin, HIGH );

  setDac( 0b11111111 );

  //Setup for relay(relayPin) and the analog output(A5) of relay circuit
  pinMode(relayPin, OUTPUT);

  //Turns the system on to begin testing
  digitalWrite(relayPin, HIGH);  // turn the relay on
  delay(timeDelay);              // wait for one second
}

//Method for DAC Output
void setDac(byte x) {

  for ( int i=0; i < pinCount; ++i ) {
    if ( bitRead( x, i ) == 1 ) {
      digitalWrite( dacOutputPin[i], HIGH );
    }
    else {
      digitalWrite( dacOutputPin[i], LOW );
    }
  }

  // latch new value
  digitalWrite( dacWRPin, LOW );
  digitalWrite( dacWRPin, HIGH );

  Serial.println("Hello Justin, Want to Play A Game?");
}


void loop(){


  //Voltage and power calculations for DAC1
  //DAC1
  int sensorValue = analogRead(VOLTS1); //intialize sensorValue as an integer and declare VOLTS to read from A0
  float dacVoltage = (float)sensorValue * (5.0 / 1023.0); //initialize voltage as a float
  
  //Power
  float power = ((dacVoltage * dacVoltage)/10000);

  float lowerVoltageLimit = 0.1;
  float upperVoltageLimit = 5.1;


  //Test Commands
  if(Serial.available() == 0){

    state = Serial.readString();
    
    //Statement to manually turn the circuit off
    if (state == "SYSOFF") {
      Serial.print("");
      digitalWrite(relayPin, LOW);
      setDac( 0b00000000 );
      Serial.println("SYSTEM IS OFF!");
    }
    //Statement to manually turn the circuit on
    if (state == "SYSON") {
      Serial.print("");
      digitalWrite(relayPin, HIGH);
      setDac( 0b10000001 );
      Serial.println("SYSTEM IS ON!");
    }
    //Statement to manually print dacVoltage
    if (state == "VDAC") {
      Serial.print("");
      Serial.print("DAC Voltage is: ");
      Serial.print(dacVoltage,4);
      Serial.println(" V");
    }
    //Statement to manually print power
    if (state == "PWR"){
      Serial.print("Power is: ");
      Serial.print(power,5);
      Serial.println(" W");
    }
    //Statement to manually print lowerVoltageLimit
    if (state == "LV") {
      Serial.print("");
      Serial.print("Lower Voltage Bound is set to: ");
      Serial.print(lowerVoltageLimit,4);
      Serial.println(" V");
    }
    //Statement to manually print upperVoltageLimit
    if (state == "HV") {
      Serial.print("");
      Serial.print("Upper Voltage Bound is set to: ");
      Serial.print(upperVoltageLimit,4);
      Serial.println(" V");
    }
  }
    //If/Print statement if voltage is out of lower bound
    if(dacVoltage < lowerVoltageLimit){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too low!");
     }
    //If/Print statement if voltage is out of higher bound
    if(dacVoltage > upperVoltageLimit){
     digitalWrite(relayPin, LOW);
     Serial.println("Voltage is too high!");
    }
    //If/Print statement if voltage is in correct range
    if ((dacVoltage >= lowerVoltageLimit) && (dacVoltage <= upperVoltageLimit)){
    digitalWrite(relayPin, HIGH);
    }  

    Serial.print("The voltage is: ");
    Serial.println(dacVoltage,4);
    delay(1000);
}

Alright so I have another question, and I'm not sure if I should start a new post but I didn't want to break a rule by doing so. The issue I'm having though is if I run this program and I set the value of the DAC using the line:

setDac( 0b11111111 );

The voltage is exactly 5.0000V, but the multimeter voltage I am reading is 4.933V. It appears that whatever I set my reference voltage to is what I return as an output voltage. If I change the 5.0V in this line:

float dacVoltage = sensorValue * (***5.0*** / 1023.0); //initialize voltage as a float

the output voltage is always equivalent to that number. I know that the output voltage cannot be higher than the reference voltage and at max it should be equal but it just seems like it's calculating the value rather than reading it from my A0 pin. For example, if I input 3.7V as that value, then the max value I return is 3.7000V and I'm ready to pull my hair out at this point.

Another interesting thing to note is that 5.0(voltage from Serial Monitor)-4.3(voltage from physical multimeter)=0.7, no matter what binary string I input into the setDac(0b________) part, the returned voltage is always 0.6-0.7V higher than what I get from the physical multimeter. I've read through the datasheet plenty of times and there is nothing specific about this error, but is there something about DACs in general that I am missing?

Thanks for the help.

I don't think you want that line in your code. You are turning A0 into a digital input but later on, you reading the analog value. Comment it out and the pin will be an analog input by default. I'm not 100% sure how they interact and can't test it at the moment.

Also, this seems silly. If there is nothing to read, then go ahead and read?

You typically only read if there is something available.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.