olmay
March 23, 2023, 5:58pm
1
Hello everyone,
I have a problem with the serial communication, since when trying to obtain the temperature data from the peltier cell, the serial communication drops, and it is blocked.
I leave the circuit used and the code.
#include <PID_v2.h>
const int limit = 10;
double pass = 0;
double measures[limit];
int index = 0;
//variables del pid
double Setpoint, Input, Output;
double Kp= 0.56984, Ki=0.3168, Kd=0.0158;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int peltier = 10;
float voltage = 0.0;
float rTemp = 0.0;
//Tiempo
int period = 5000;
int nDelay = 100;
long timeNew = 0;
void setup() {
Serial.begin(9600);
//Salida peltier
pinMode(peltier,OUTPUT);
//Configuracion PID
Setpoint = 35;
myPID.SetMode(AUTOMATIC);
}
void loop() {
timeNew = millis();
rTemp = real_temp();
Input = rTemp;
myPID.Compute();
analogWrite(peltier,Output);
voltage = (Output / 255) * 5;
if(Input >= 35){
cycleWork();
}
sendData();
while(millis() < timeNew + nDelay){}
}
void cycleWork(){
if(Input > Setpoint){
while(millis() < timeNew + period){}
change();
}
}
void sendData(){
Serial.print(Input);
Serial.print(",");
Serial.println(voltage);
}
void change(){
Setpoint += 1;
Serial.print("change from point to: ");
Serial.println(Setpoint);
}
double real_temp(){
for(int i = 0; i < limit; i++){
media(temp_cupla());
}
return pass;
}
void media(float valor){
double me = 0;
if(index == limit){
for(int i = 0; i < 10; i++){
me = me + measures[i];
}
index = 0;
me = me / limit;
pass = me;
}else{
measures[index] = valor;
index = index + 1;
}
}
float temp_cupla(){
float temp = 0.0;
int value = analogRead(A1);
float millivolts = (value / 1023.0) * 5000;
temp = millivolts / 10;
return temp;
}
pylon
March 23, 2023, 6:41pm
2
Remove that line, it might block for a too long time. If you think you need it, use delay() instead.
What does that mean? Does it stop sending the values or does the signal drop (TX goes low)?
If it's the first, the reason is probably your try to replace delay() by a worse construct.
Redesign the code a littlle. Remove the blocking while. Instead, run the logic needed when millis() and the time constants matches activity time.
olmay
March 24, 2023, 5:10pm
4
I already used the delay() function and it's still the same, the serial monitor shows some data and gets stuck and stops working.
I already use other programs like matlab and python to get the data and still the same.
What is the box labeled "IRF520"? Why is it connected to a 5V power supply?
If that is a board with an IRF520 MOSFET, it won't work well or at all with a 5V Arduino, especially with a Peltier module.
olmay
March 24, 2023, 5:52pm
6
Why? I will use the pwm, I do not require the logical state,
but, what do you recommend as a replacement?
The IRF520 is not a logic level MOSFET, and is designed to be fully turned on when 10V is on the gate. It is not designed to work with 5V Arduinos.
Use the IRL 540 or another logic level MOSFET instead, like this one .
olmay
March 24, 2023, 6:11pm
8
I'll take it into account, but the problem is not that the module doesn't work for me, but rather that the serial connection is blocked.
The mysterious, undefined module may be somehow interfering with Arduino function.
Remove it completely and retest the serial output.
Also, correct your usage of millis(). This is not correct at all, and is worse than using delay() because it can fail completely.
while(millis() < timeNew + period){}
How are you powering the arduino?
Using UNO and powering through P10, with 5V it will not work.
Power through P10 passes through a diode and a regulator that needs about 7V to output 5V.
look for the correct name of this connector.
olmay
March 24, 2023, 7:08pm
11
I use the same usb cable to feed the same arduino, I don't use a source, because I require data from the arduino from the serial monitor.
Sorry,
I confused pin P10 with the connector that sometimes someone calls P10.
Post a complete, correct wiring diagram. The one you posted is useless.
Don't forget to try the experiment in post #9 .
system
Closed
October 9, 2023, 3:14pm
15
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.