How to generate triangle 12bit triangle signal frequency of 70Hz with 4-channel MCP4728 and Arduino Mega?
The search phrase "arduino triangle wave dac" finds several tutorials.
Thank you for your advice. I need to have 4000 steps for 40 ms. I have 4000 steps for 2 s.
I share my code with you.
#include <Adafruit_MCP4728.h>
#include <Wire.h>
//#include <eRCaGuy_Timer2_Counter.h>
Adafruit_MCP4728 mcp;
unsigned long previousMicros = 0; // Variable to store the last time the wave was updated
unsigned long period = 10;
int waveValue = 0; // Current value of the wave
int increment = 1; // Increment value for generating the triangle wave
int minimum = 0;
int maksimum = 4000;
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
while (!Serial1)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
if (!mcp.begin())
{
while (1) {
delay(10);
}
}
//timer2.setup();
mcp.setChannelValue(MCP4728_CHANNEL_A, 0);
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
mcp.setChannelValue(MCP4728_CHANNEL_C, 0);
mcp.setChannelValue(MCP4728_CHANNEL_D, 0);
}
void loop() {
// put your main code here, to run repeatedly:
//unsigned long t_start = timer2.get_count();
unsigned long currentMicros = micros();
if (currentMicros - previousMicros >= period) { // If it's time to update the wave
previousMicros = currentMicros; // Update the previous time
waveValue += increment; // Increment or decrement the wave value
if (waveValue == minimum || waveValue == maksimum) { // Change direction at the ends of the range
increment = -increment;
}
mcp.setChannelValue(MCP4728_CHANNEL_A, waveValue);
}
}
Please edit your post to add code tags. See the "How to get the best out of this forum" post for instructions.
You will need to send data to the DAC 50X faster. Is that even possible with that DAC and whatever Arduino you are using?
In the IDE use "copy for forum" and paste your code here.
It's on the Menu under EDIT
Thanks, I edited my post.
I think I have a problem with the I2C speed. In my case, the limits are for Arduino mega and DAC (max. 3.4 MHz).
I trust that someone will check my code and help solve the problem.
Alternative setup()
void setup()
{
Serial1.begin(115200);
while (!Serial1) // will pause Zero, Leonardo, etc until serial console opens
Wire.begin(); // start I2C
Wire.setClock(800000); // tune this
Serial1.println("I2C started");
if (!mcp.begin())
{
Serial1.println("Failed to start mcp");
while (1);
}
mcp.setChannelValue(MCP4728_CHANNEL_A, 0);
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
mcp.setChannelValue(MCP4728_CHANNEL_C, 0);
mcp.setChannelValue(MCP4728_CHANNEL_D, 0);
}
You could, for example, send 10X fewer points (steps of 10 rather than 1) to decrease the ramp time by 10X.
Not possible with a Mega!
Best you could do is about 400 steps in 40ms
What do you suggest as a replacement for Mega?
@dencevski
You would need an Arduino that can implement the I2C High-speed mode (3.4Mbits/s) and I don't think that there are any Arduinos that can, except maybe the Adafruit Metro M4
However, it's highly unlikely that you could make it work in a breadboard set-up
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.