ADC and DAC affect each other

mkremer
Posts: 9
Joined: Sun Feb 27, 2022 6:36 pm

ADC and DAC affect each other

Postby mkremer » Sat Aug 06, 2022 9:36 pm

When i use the Arduino commands "analogRead()" (read adc, analog input) and directly after this "dacWrite()" (write to dac, analog output), then it works well.
But when i use the esp-idf commands "int val = adc1_get_raw(ADC1_CHANNEL_0);" and directly after this "dac_output_voltage(DAC_CHANNEL_1, value);" then it doesn't work.

ADC and DAC doesn't work together in one task! Why not? Can someone from Espressif please help me in this case? I cannot find solutions for this promblem on the "get started" page.

When i use ADC and DAC in independent tasks, then it works perfectly, but why not in one task? Where is the problem? Are there timing problems or do dac and adc need to be disabled? I dont know...

I use the original "ESP32-DevKitC-32D" from Espressif, but with a board from AZ-Delivery i have exactly the same problems.

------------------------------------------------------------------------------------------

Hello, i have a little code for communication with a simulation software on pc.
The Code works well, when i comment out the part with the adc or with the dac. But together it does not work.
Could it be that it is because of the configuration of the adc oder the dac?

And the two adc Channels affect each other, too. When i connect a potentiometer to adc1, then adc2 reacts too.

Can someone help me, what i'am doing wrong here?

I was planning to convert arduino code to esp idf. the working arduino code is under point 2.

------- 1.) ------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "setup.h"
#include "driver/dac.h"
#include "driver/adc.h"

#define BUF_SIZE 1024
#define uart UART_NUM_0
#define gpio4 GPIO_NUM_4
#define gpio5 GPIO_NUM_5
#define btn1 15
#define btn2 32
#define high 1
#define low 0

static void task()
{
uint8_t data[6];

while(1)
{
// Read data from uart
int anz_bytes = uart_read_bytes(uart, data, BUF_SIZE, 10 / portTICK_RATE_MS);

if(anz_bytes > 2) // Wenn mindestens 3 Bytes im Puffer.
{
// ------------------------------- //
//---------- 1. Ausgabe ---------- //
// ------------------------------- //
if(data[0] == '!')
{
// ------------- Teil 1.1 ------------ //
// -- Ausgabe an Digitale Ausgänge - //
if(data[1] == 'D')
{
if(data[2] == '1'){
if(data[3] == '1')
gpio_set_level(gpio4, high);
else
gpio_set_level(gpio4, low);
}
if(data[2] == '2'){
if(data[3] == '1')
gpio_set_level(gpio5, high);
else
gpio_set_level(gpio5, low);
}
}

// ------------- Teil 1.2 ---------- //
// -- Ausgabe an Analoge Ausgänge -- //
if(data[1] == 'A')
{
char analog[3] = "000"; // Für die analoge Ausgabe am ESP32 / DAC
int i;

for(i=0; i<=2; i++){
analog = data[i+3];
}

if(data[2] == '1'){
// Daten von 0...255 zum DAC senden
int value1 = atoi(analog); // string to int
dac_output_voltage(DAC_CHANNEL_1, value1); // Analoge Spannung an Pin 25
}

if(data[2] == '2'){
// Daten von 0...255 zum DAC senden
int value2 = atoi(analog); // string to int
dac_output_voltage(DAC_CHANNEL_2, value2); // Analoge Spannung an Pin 26
}
}
uart_write_bytes(uart, "OK\r\n", 4);
}
// ------------------------------- //
//----------- 2. Lesen ----------- //
// ------------------------------- //
// Nächster Schritt am 04.08.2022
// Analog Eingang
if(data[0] == '?' && data[1] == 'A')
{
// Werte von 0...4095 eingeben.
if(data[2] == '1'){
char eingabe[5];
int val = adc1_get_raw(ADC1_CHANNEL_0);
sprintf(eingabe, "%i", val);
uart_write_bytes(uart, eingabe, 4);
uart_write_bytes(uart, "\r\n", 2);
//uart_write_bytes(uart, "1111\r\n", 6)
}
if(data[2] == '2')
{
char eingabe1[5];
int val = adc1_get_raw(ADC1_CHANNEL_3);
sprintf(eingabe1, "%i", val);
uart_write_bytes(uart, eingabe1, 4);
uart_write_bytes(uart, "\r\n", 2);
//uart_write_bytes(uart, "1210\r\n", 6)
}
}

// Digital Eingang
if(data[0] == '?' && data[1] == 'D')
{
// 0 oder 1 eingeben.
if(data[2] == '1'){
if(gpio_get_level(btn1) == 0)
uart_write_bytes(uart, "1\r\n", 3);
else
uart_write_bytes(uart, "0\r\n", 3);
}

if(data[2] == '2'){
if(gpio_get_level(btn2) == 0)
uart_write_bytes(uart, "1\r\n", 3);
else
uart_write_bytes(uart, "0\r\n", 3);
}
}
}
}

}

void app_main()
{
int baudrate = 115200; // Baudrate später per Tasten auswählbar.

setup_adc_dac();
setup_uart(baudrate);
setup_gpio();

xTaskCreate(task, "task", 1024, NULL, 1, NULL);

}

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/dac.h"
#include "driver/adc.h"

#define TXD_PIN 1
#define RXD_PIN 3

#define BUF_SIZE 1024
#define uart UART_NUM_0

void setup_uart(int baudrate){
uart_config_t uart_config = {
.baud_rate = baudrate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
/.rx_flow_ctrl_thresh = 122,/
};

uart_param_config(uart, &uart_config);
uart_set_pin(uart, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart, BUF_SIZE * 2, 0, 0, NULL, 0);

}

void setup_gpio(){
gpio_set_direction(4,GPIO_MODE_OUTPUT);
gpio_set_direction(5,GPIO_MODE_OUTPUT);

//Test
gpio_set_direction(22,GPIO_MODE_OUTPUT);


gpio_set_direction(15,GPIO_MODE_INPUT);
gpio_set_direction(32,GPIO_MODE_INPUT);

gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // Benötigt, bei Verwendung von Tastern
gpio_set_pull_mode(32, GPIO_PULLUP_ONLY); // Benötigt, bei Verwendung von Tastern

}

void setup_adc_dac(){
dac_output_enable(DAC_CHANNEL_1);
dac_output_enable(DAC_CHANNEL_2);

adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11);
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);

}





--------- 2.) ------- Arduino Code

const uint8_t DOUT1_PIN = 4;
const uint8_t DOUT2_PIN = 15;
const uint8_t AIN1_PIN = 32;
const uint8_t AIN2_PIN = 33;
const uint8_t DIN1_PIN = 34;
const uint8_t DIN2_PIN = 35;

void setup()
{
pinMode(DOUT1_PIN, OUTPUT);
pinMode(DOUT2_PIN, OUTPUT);
pinMode(DIN1_PIN, INPUT);
pinMode(DIN2_PIN, INPUT);
pinMode(AIN1_PIN, INPUT);
pinMode(AIN2_PIN, INPUT);
Serial.begin(38400);
}

// Kommandos von BORIS:
//
// !Dxy : Setze Digitalausgang x (x = 1,2) auf y (y = 0,1)
// !Axyyy : Setze Analogausgang x (x = 1,2) auf yyy (yyy = 000..255)
// ?Dx : Liefere Wert von Digitaleingang x (x = 1,2), Rückgabewert vom Arduino: 0,1
// ?Ax : Liefere Wert von Analogeingang x (x = 1,2), Rückgabewert vom Arduino: 0..4095

void loop()
{
while (Serial.available() && (Serial.peek() != '!') and (Serial.peek() != '?')) {Serial.read();} // auf ! oder ? warten
if (Serial.available() > 2)
{
char firstChar = (char)Serial.read();
// Wert setzen
if (firstChar == '!')
{
char varType = (char)Serial.read();
char varIndex = (char)Serial.read();
while (Serial.available() < 1) {};
if (varType == 'D')
{
char varDValue = (char)Serial.read();
if (varIndex == '1')
{
if (varDValue == '0') {digitalWrite(DOUT1_PIN, LOW);} else {digitalWrite(DOUT1_PIN, HIGH);}
}
if (varIndex == '2')
{
if (varDValue == '0') {digitalWrite(DOUT2_PIN, LOW);} else {digitalWrite(DOUT2_PIN, HIGH);}
}
}
if (varType == 'A')
{
String valueStringInt = "000";
while (Serial.available() < 3) {};
valueStringInt.setCharAt(0, (char)Serial.read());
valueStringInt.setCharAt(1, (char)Serial.read());
valueStringInt.setCharAt(2, (char)Serial.read());
if (varIndex == '1') {dacWrite(DAC1, valueStringInt.toInt());}
if (varIndex == '2') {dacWrite(DAC2, valueStringInt.toInt());}
}
Serial.println("OK");
}
// Wert lesen
if (firstChar == '?')
{
char varType = (char)Serial.read();
char varIndex = (char)Serial.read();
if (varType == 'A')
{
if (varIndex == '1') {Serial.println(analogRead(AIN1_PIN));}
if (varIndex == '2') {Serial.println(analogRead(AIN2_PIN));}
}
if (varType == 'D')
{
if (varIndex == '1') {Serial.println(digitalRead(DIN1_PIN));}
if (varIndex == '2') {Serial.println(digitalRead(DIN2_PIN));}
}
}
}
}

rdeletric
Posts: 1
Joined: Tue Aug 01, 2023 1:57 am

Re: ADC and DAC affect each other

Postby rdeletric » Tue Aug 01, 2023 2:01 am

Hi friend, did you find a solution? I also have this problem.

Who is online

Users browsing this forum: No registered users and 131 guests