/*
In this tutorial we will [attempt to] connect a MCP4725 DAC IC with Arduino Uno
and provide analog input value to Arduino pin A0 by using a MAX6675 SPI thermocouple module.
https://circuitdigest.com/microcontroller-projects/arduino-dac-tutorial-interfacing-mcp4725-dac
*/
// Include dependant SPI Library
#include <SPI.h>
// Include Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "max6675.h"
// Define Constants
int gndPin2 = 22;// Gnd = Ground pin
int vccPin2 = 24;// Vcc = Power pin
int thermoCLK = 26;// SCK = Serial Clock pin
int thermoCS = 28; // CS = Chip Select pin
int thermoDO = 30; // SO = Serial Out pin
// Initialize MAX6675 sensor for normal 16mhz Arduino
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
#include<Wire.h> //Include Wire library for using I2C functions
#include <LiquidCrystal_I2C.h> //Include LCD library for using LCD display functions
#define MCP4725 0x60 //MCP4725 address as 0x60 Change yours accordingly
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);// use I2C Scanner to find the address: 0x27 or something like that
unsigned int adc;
byte buffer[3];
// Define Constants
const int gndPin1 = 50;// Gnd = Ground pin
const int vccPin1 = 52;// Vcc = Power pin
const int a = 32; // Set fom 0 to 255 for a given brightness via PWM
const int LCDPin = 3; // the pin that the LCD LED is attached to PWM pin 3
void setup() {
// use Arduino pins
pinMode(gndPin1, OUTPUT); digitalWrite(gndPin1, LOW); // declare Gnd pin
pinMode(vccPin1, OUTPUT); digitalWrite(vccPin1, HIGH); // declare Vcc pin
pinMode(gndPin2, OUTPUT); digitalWrite(gndPin2, LOW); // declare Gnd pin
pinMode(vccPin2, OUTPUT); digitalWrite(vccPin2, HIGH); // declare Vcc pin
pinMode(LCDPin, OUTPUT); // declare pin 3 to be a PWM output
Wire.begin(); //Begins the I2C communication
lcd.begin(); //Sets LCD in 20X4 Mode
lcd.print("CIRCUIT DIGEST");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Arduino");
lcd.setCursor(0, 1);
lcd.print("DAC with MCP4725");
delay(2000);
lcd.clear();
}
void loop() {
delay(500); // Delay so MAX6675 sensor can stabalize
analogWrite(LCDPin, a); // set the brightness of PWM pin 3:
buffer[0] = 0b01000000; //Sets the buffer0 with control byte (010-Sets in Write mode)
adc = analogRead(A0) * 4; //Read Analog value from pin A0 and convert into digital (0-1023) multiply with 4 gives (0-4096)
float ipvolt = (5.0 / 4096.0) * adc; //Finding voltage formula (A0)
buffer[1] = adc >> 4; //Puts the most significant bit values
buffer[2] = adc << 4; //Puts the Least significant bit values
unsigned int analogread = analogRead(A1) * 4 ; //Reads analog voltage from A1 [Need to multiply by 4 for the 0-4096 bit reason]
float opvolt = (5.4 / 4096.0) * analogread; //Finding Voltage Formula (A1) [Chose 5.4 to make readings match]
Wire.beginTransmission(MCP4725); //Joins I2C bus with MCP4725 with 0x60 address
Wire.write(buffer[0]); //Sends the control byte to I2C
Wire.write(buffer[1]); //Sends the MSB to I2C
Wire.write(buffer[2]); //Sends the LSB to I2C
Wire.endTransmission(); //Ends the transmission
lcd.setCursor(0, 0);
lcd.print("A IP:");
lcd.print(adc); //Prints the ADC value from A0
lcd.setCursor(10, 0);
lcd.print("V:"); //Prints the Input Voltage at A0
lcd.print(ipvolt);
lcd.setCursor(0, 1);
lcd.print("D OP:");
lcd.print(analogread); //Prints the ADC value from A1 (From DAC)
lcd.setCursor(10, 1);
lcd.print("V:");
lcd.print(opvolt); //Prints the Input Voltage at A1 (From DAC)
lcd.setCursor(0, 2);
lcd.print("F = ");
lcd.print(thermocouple.readFahrenheit());
lcd.print("C = ");
lcd.print(thermocouple.readCelsius());
delay(500);
lcd.clear();
}
I know that the SPI buss data can be somehow fed to the DAC buffer in real time, but the question is "how?'. Figuring out how to allocate the bits is tricky to me and I've searched high and low online with minimal references to this issue.
I know that I'm attempting to seamlessly mesh an SPI device to an I2C device and I am humbled by those willing to point me in the right direction.

