top of page
Writer's pictureRAJ SHARMA

RC Signal to Voltage Converter, RC In DAC MCP4725 Output



Arduino Code Without Limit Switch

.......................................................................................................


// Include the Wire library for I2C communication

#include <Wire.h>


// Define the address of the MCP4725 DAC

#define MCP4725_ADDR 0x60


// Define the minimum and maximum RC signal values

#define RC_MIN 1030

#define RC_MAX 2050


// Define the minimum and maximum DAC output values

#define DAC_MIN 0

#define DAC_MAX 4096


// Define variables to store the RC signal and DAC output values

int rcSignal;

int dacOutput;


void setup() {

// Initialize the serial communication for debugging

Serial.begin(9600);


// Initialize the I2C communication

Wire.begin();


// Set the MCP4725 to normal mode

Wire.beginTransmission(MCP4725_ADDR);

Wire.write(0x00);

Wire.endTransmission();

}


void loop() {

// Read the RC signal value

rcSignal = pulseIn(2, HIGH);


// Map the RC signal value to the DAC output range

dacOutput = map(rcSignal, RC_MIN, RC_MAX, DAC_MIN, DAC_MAX);


// Send the DAC output value to the MCP4725

Wire.beginTransmission(MCP4725_ADDR);

Wire.write(0x40); // Write to DAC register

Wire.write(dacOutput >> 4); // Send the high byte

Wire.write(dacOutput & 0xFF); // Send the low byte

Wire.endTransmission();


// Print the RC signal and DAC output values for debugging

Serial.print("RC Signal: ");

Serial.print(rcSignal);

Serial.print(" | DAC Output: ");

Serial.println(dacOutput);


// Delay for 100 milliseconds

delay(100);

}



..............................................................................................................................

Arduino Code with Limit Switch

// Include the Wire library for I2C communication

#include <Wire.h>


// Include the MCP4725 library for DAC control

#include <Adafruit_MCP4725.h>


// Define the MCP4725 DAC object

Adafruit_MCP4725 dac;


// Define the pin for the limit switch

#define LIMIT_SWITCH_PIN 9


// Define the variables for RC signal and DAC output

int rcSignal;

int dacOutput;


void setup() {

// Initialize the serial monitor for debugging

Serial.begin(9600);


// Initialize the MCP4725 DAC object with the default address

dac.begin(0x60);


// Set the limit switch pin as input

pinMode(LIMIT_SWITCH_PIN, INPUT);

}


void loop() {

// Read the RC signal using pulseIn function

rcSignal = pulseIn(2, HIGH);


// Map the RC signal range (1000-2000) to the DAC output range (0-4096)

dacOutput = map(rcSignal, 1030, 2050, 0, 4096);


// Check if the limit switch is high

if (digitalRead(LIMIT_SWITCH_PIN) == HIGH) {

// Output 0 to the DAC

dac.setVoltage(0, false);

} else {

// Output the mapped RC signal to the DAC

dac.setVoltage(dacOutput, false);

}


// Print the RC signal and DAC output for debugging

Serial.print("RC Signal: ");

Serial.print(rcSignal);

Serial.print(" | DAC Output: ");

Serial.println(dacOutput);


// Add a delay for stability

delay(100);

}




.............................................................................................................................

Controllino Code

#include <Wire.h> // Include the Wire library for I2C communication

#include <Adafruit_MCP4725.h> // Include the Adafruit library for the MCP4725 DAC


#define RC_SIGNAL_PIN 5 // Define the RC signal input pin (change this to your specific pin)

#define LIMIT_SWITCH_PIN 3 // Define the limit switch pin (change this to your specific pin)


Adafruit_MCP4725 dac; // Create an instance of the MCP4725 DAC


void setup() {

pinMode(RC_SIGNAL_PIN, INPUT); // Set the RC signal pin as an input

pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP); // Set the limit switch pin as an input with internal pull-up resistor

dac.begin(0x60); // Initialize the DAC with the I2C address (0x60 is the default address)

Serial.begin(9600); // Initialize serial communication for debugging

}


void loop() {

// Check if the limit switch is pressed

if (digitalRead(LIMIT_SWITCH_PIN) == LOW) {

// If the limit switch is pressed, set the DAC output to 0V

dac.setVoltage(0, false);

Serial.println("Limit switch hit, DAC output set to 0V");

} else {

// Read the RC signal pulse width in microseconds

unsigned long pulseWidth = pulseIn(RC_SIGNAL_PIN, HIGH);


// Map the pulse width to the DAC output range (0 to 4095)

int dacValue = map(pulseWidth, 2100, 1530, 0, 4095); // Adjust 1000 and 2000 to your RC signal range


// Set the DAC output

dac.setVoltage(dacValue, false);


// Print the values for debugging

Serial.print("Pulse Width: ");

Serial.print(pulseWidth);

Serial.print(" us, DAC Value: ");

Serial.println(dacValue);

}

delay(10); // Small delay to avoid overwhelming the DAC with rapid updates

}






3 views0 comments

Recent Posts

See All

Comments


bottom of page