Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
esp32, MCP3425 I2C ADC test
/*
esp32, MCP3425 I2C ADC test.
*/
#include<Wire.h>
//extern "C" {
//#include <user_interface.h>
//}
// MCP3425 I2C address is 0x68(104)
#define Addr 0x68
//const char* ssid = "";
//const char* password = "";
//const char* host = "api.thingspeak.com";
//String mAPI_KEY="your-KEY";
static uint32_t mTimerTmp=0;
static uint32_t mTimerPos=0;
float mTemp=0;
float mHum=0;
//
int get_tempValue(){
int iRet=0;
unsigned int data[2]= { 0x00 , 0x00};
Wire.beginTransmission(Addr);
Wire.write(0x00);
Wire.endTransmission();
//
Wire.requestFrom(Addr, 2);
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data to 12-bits
int raw_adc = (data[0] & 0x0F) * 256 + data[1];
//Serial.println("adc.Before=" + String(raw_adc ) + " mV");
if(raw_adc > 2047)
{
raw_adc -= 4096;
}
Serial.print("Digital Value of Analog Input : ");
Serial.println(raw_adc);
//Sensor
// int iTemp = (raw_adc - 424) / 6.25; //電圧値を温度に変換, offset=425
//Serial.println( "iTemp=" + String(iTemp ) );
// iRet= iTemp;
return iRet;
}
//
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin( 115200 );
Serial.println("#Start-setup");
// Start I2C Transmission
Wire.beginTransmission(Addr);
Wire.write(0x10);
Wire.endTransmission();
delay(300);
}
//
void loop() {
int iTemp =0;
if( millis() > mTimerTmp){
mTimerTmp += 5000;
iTemp =get_tempValue();
}
delay( 100 );
// Serial.println("t="+ String(millis() ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.