本教程是的一部分: 使用MCP4725 DAC数模转换器和Arduino(5个项目)
所有关于MCP4725 DAC数模转换器的教程。 其他视频的链接在本文下方。
Lesson 109-3: Convert PWM Duty Cycle to Voltage Using MCP4725 12-Bit DAC with Arduino
In this lesson, we learn how to use the MCP4725 12-bit Digital-to-Analog Converter (DAC). The chip and module are explained, the datasheet is reviewed, and then, using five projects, this MCP4725 is practically shown. Full wiring diagrams are shown, and how to use the code and library is explained.
In Project 3 in lesson 109, convert a PWM duty cycle to voltage using the MCP4725 12-bit DAC with an Arduino.
本教程是……的一部分: 使用MCP4725 DAC数模转换器和Arduino(5个项目)
- Lesson 109-1: How to Use the MCP4725 DAC Outputting DC, Sine/Triangular Waves Using Arduino
- Lesson 109-2: Controlling the Output Voltage of the MCP4725 from Serial Monitor
- Lesson 109-4: Using Two MCP4725 DAC Modules with an Arduino
- Lesson 109-5: Setting Output Voltage with a Push Button Using an MCP4725 DAC with Arduino
381-Lesson 109: Generating DC Voltage Using an MCP4725 DAC with LCD and PWM-to-Voltage Converter with Arduino (5 projects)
语言: C++
/*
* Lesson 109-3: How to convert PWM to Voltage using MCP4725 DAC with Arduino
* This Arduino project converts PWM to Voltage using MCP4725.
* The output voltage will be a maximum of 5.5V (2.7V to 5.5V) or whatever you
* power it with.
*
* Watch video instructions for this code: https://youtu.be/j_BwZT9z-0g
Projects in this video: https://youtu.be/j_BwZT9z-0g
Project 1: Output 0 to 5V or 0 to 3V depending on which kind of power source you use for the module. The output is shown on a digital multimeter.
Project 2: Serial command to set voltage
Project 3 (this project): PWM duty cycle to Voltage converter
Project 4: Using two MCP4725 DAC modules
Project 5: Using a push button to set voltage on LCD
* Written by Ahmad Shamshiri using Adafruit library for MCP4725
Sep 26, 2022
www.Robojax.com
*
* This code is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
*
* For the library for this code, visit http://robojax.com/
*
If you found this tutorial helpful, please support me so I can continue creating
content like this. Make a donation using PayPal by credit card https://bit.ly/donate-robojax
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Based on the Adafruit MCP4725 Library
----> http://www.adafruit.com/products/935
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
#include <Wire.h>
#include <Adafruit_MCP4725.h>
int refV = 4530;//set the voltage source in millivolts
Adafruit_MCP4725 dac;
#define pwmReadPin 2//the pin you are going to read PWM signal from
int averageCounts= 50;//for PWM duty cucle
/// do not change below this
int highTime =0;
int lowTime=0;
float dutyCycle =0;
int averageDutyCycle=0;
int mV;
float V;
void readDutyCycle(void);
void average(void);
void setV(void);
void setmV(void);
void setup(void) {
Serial.begin(9600);
Serial.println("PWM to Voltage Coverter");
pinMode(pwmReadPin, INPUT);
//run i2C scanner to find out the I2C address and enter it below
//see video for details https://youtu.be/j_BwZT9z-0g
//get more code from www.Robojax.com
dac.begin(0x60);
Serial.println("www.Robojax.com");
}
void loop(void) {
readDutyCycle();//read the duty cycle
pwmToVoltage();
//printFulleData();
// Serial.print("PWM Dutycycle: ");
// Serial.print(averageDutyCycle);
// Serial.print("%");
// Serial.print(" is: ");
// Serial.print(mV);
// Serial.println("mV");
if(averageDutyCycle >30)
{
//do something here
//Serial.println("Duty cycle is above 30");
}
//delay(500);
//see video for details https://youtu.be/j_BwZT9z-0g
//get more code from www.Robojax.com
}//loop end
void pwmToVoltage()
{
//see video for details https://youtu.be/j_BwZT9z-0g
//get more code from www.Robojax.com
setmV(map(averageDutyCycle, 0, 100, 0, refV));
}
/*
* setmV()
* sets mV value such as 35mV or 2654mV (2.654V)
* Written Sep 25, 2022
* by Ahmad Shamshiri www.Robojax.com
*/
void setmV(int d)
{
mV =d;
dac.setVoltage(map(d, 0, refV, 0, 4095), false);
}
/*
* setV()
* sets Voltage value in volts such as 3.764
* Written Sep 25, 2022
* by Ahmad Shamshiri www.Robojax.com
*/
void setV(float d)
{
V = d;
dac.setVoltage( map((d *1000), 0, refV, 0, 4095), false);
}
void readDutyCycle(){
highTime = pulseIn(pwmReadPin, HIGH);
lowTime = pulseIn(pwmReadPin, LOW);
dutyCycle = round((float)highTime /( (float)highTime + (float)lowTime)* 100);
average();
}
void average()
{
int sumValue=0;//variable to store sum
for(int i=0; i< averageCounts; i++)
{
sumValue +=dutyCycle;//add each value to the sumValue
}
if(averageCounts >0){
averageDutyCycle = sumValue /averageCounts;//calculate the average and update the variable
}
//see video for details https://youtu.be/j_BwZT9z-0g
//get more code from www.Robojax.com
}//average end
void printFulleData()
{
Serial.print("High: ");
Serial.print(highTime);
Serial.print("?S Low: ");
Serial.print(lowTime);
Serial.print("?S dutyCycle: ");
Serial.print(dutyCycle);
Serial.print("% Average: ");
Serial.print(averageDutyCycle);
Serial.println("%");
}
|||您可能需要的东西
-
易趣Purchase MCP4725 DAC from eBayebay.us
-
全球速卖通Purchase MCP4725 DAC from AliExpresss.click.aliexpress.com
-
BanggoodPurchase LCD1602 display from Banggoodbanggood.com
资源与参考
-
外部
-
外部
-
外部
-
外部Purchase LCD1602 display from Banggoodbanggood.com
-
外部Purchase MCP4725 DAC from AliExpresss.click.aliexpress.com
-
外部Purchase MCP4725 DAC from eBayebay.us
-
外部
-
外部
-
外部
文件📁
没有可用的文件。