我使用MCP9600传感器来了解温度。我使用Python2560,在我的笔记本电脑上使用ATmega与库串行通信。
这是我的arduino
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include "Adafruit_MCP9600.h"
#include <SimpleCLI.h>
#define I2C_ADDRESS (0x67)
Adafruit_MCP9600 mcp;
uint8_t filtcoeff;
// Create CLI Object
SimpleCLI cli;
// Commands
Command type;
Command filter;
Command temp;
Command fanspeed;
Command Tmax;
Command tolerance;
//Call function
void overheat();
void deltatemp();
// Callback function
void typeCallback();
void filterCallback();
void temperatureCallback();
void FanCallback();
void overheatCallback();
void toleranceCallback();
int broche_PWM = 2; // Déclaration de la Pin 10 Arduino
int Valeur_PWM = 128; // Variable
int timedelay = 300;
int tempTolerance = 10;
int tempmax = 50;
void setup() {
// put your setup code here, to run once:
pinMode(broche_PWM, OUTPUT); // Pin 2 Arduino en sortie PWM
Serial.begin(115200);
while (!Serial) {
delay(10);
}
cli.setOnError(errorCallback); // Set error Callback
/* Initialise the driver with I2C_ADDRESS and the default I2C bus. */
if (! mcp.begin(I2C_ADDRESS)) {
Serial.println("Sensor not found. Check wiring!");
while (1);
}
mcp.setADCresolution(MCP9600_ADCRESOLUTION_12);
switch (mcp.getADCresolution()) {
case MCP9600_ADCRESOLUTION_18: ; break;
case MCP9600_ADCRESOLUTION_16: ; break;
case MCP9600_ADCRESOLUTION_14: ; break;
case MCP9600_ADCRESOLUTION_12: ; break;
}
mcp.setThermocoupleType(MCP9600_TYPE_K);
switch (mcp.getThermocoupleType()) {
case MCP9600_TYPE_K: ; break;
case MCP9600_TYPE_J: ; break;
case MCP9600_TYPE_T: ; break;
case MCP9600_TYPE_N: ; break;
case MCP9600_TYPE_S: ; break;
case MCP9600_TYPE_E: ; break;
case MCP9600_TYPE_B: ; break;
case MCP9600_TYPE_R: ; break;
}
mcp.setFilterCoefficient(3);
mcp.setAlertTemperature(1, 30);
mcp.configureAlert(1, true, true); // alert 1 enabled, rising temp
mcp.enable(true);
type = cli.addBoundlessCommand("type", typeCallback);
filter = cli.addBoundlessCommand("filter", filterCallback);
temp = cli.addBoundlessCommand("temp", temperatureCallback);
fanspeed = cli.addBoundlessCommand("fanspeed", FanCallback);
Tmax = cli.addBoundlessCommand("Tmax", overheatCallback);
tolerance = cli.addBoundlessCommand("Tolerance", toleranceCallback);
Serial.println("I am ready"); // avoid to have a void monitor (avoiding blocking in python code)
}
void loop() {
// put your main code here, to run repeatedly:
// Check if user typed something into the serial monitor
if (Serial.available()) {
// Read out string from the serial monitor
String input = Serial.readStringUntil('\n');
// Parse the user input into the CLI
cli.parse(input);
}
if (cli.errored()) {
CommandError cmdError = cli.getError();
Serial.print("ERROR: ");
Serial.println(cmdError.toString());
if (cmdError.hasCommand()) {
Serial.print("Did you mean \"");
Serial.print(cmdError.getCommand().toString());
Serial.println("\"?");
}
}
if (mcp.readThermocouple() > tempmax){
overheat();
}
if (abs(mcp.readThermocouple()-mcp.readAmbient())> tempTolerance){
//deltatemp();
}
analogWrite(broche_PWM,Valeur_PWM); // Envoi du signal PWM sur la sortie numérique 10
delay(timedelay);
}
void temperatureCallback(cmd* c){
Command cmd(c); // Create wrapper object
Serial.print("Hot Junction: ");
Serial.print("x");
Serial.print(mcp.readThermocouple());
Serial.print("x");
Serial.print("Cold Junction: ");
Serial.print("x");
Serial.print(mcp.readAmbient());
Serial.print("x");
Serial.print("ADC (uV): ");
Serial.print("x");
Serial.println(mcp.readADC() * 2);
}
void filterCallback(cmd* c){
Command cmd(c); // Create wrapper object
Argument arg;
String argValue = "";
int i=0;
int IntValue;
arg = cmd.getArg(i);
argValue = arg.getValue();
IntValue = argValue.toInt();
mcp.setFilterCoefficient(IntValue);
Serial.print("Filter coefficient value set to: ");
Serial.println(mcp.getFilterCoefficient());
}
}和python代码,我用它从ATmega中提取数据
import numpy as np
import serial
import time
def get_temperature(serial_port):
phrase = b'temp'
# print("str.encode(phrase)",str.encode(phrase))
serial_port.write(phrase)
exitloop = 0
while exitloop == 0:
if serial_port.inWaiting() > 0:
arduino_data = serial_port.readline()
print(arduino_data)
serial_port.flushOutput()
exitloop = 1
serial_port.flushOutput()
serial_port.flushInput()
return arduino_data热电偶是K型的。问题是,当我从arduino串行监视器和我的python代码中看到,来自传感器的值在很长一段时间内都是恒定的。通常,当我与另一台设备进行比较时,该值必须不断变化,但在很长一段时间内,如5分钟,该值必须相同。
有人能给出解决方案吗?
发布于 2021-09-13 09:00:29
我找到问题了。它来自ADC_resolution,我在18岁的时候还回来了。
谢谢
https://stackoverflow.com/questions/69141885
复制相似问题