Last active
March 25, 2019 13:04
-
-
Save Conplug/88131ac6b9e712c22855c7b687f928e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright (c) 2019 Conplug (https://conplug.com.tw) | |
// Author: Hartman Hsieh | |
// | |
// Description : | |
// None | |
// | |
// Connections : | |
// Plug "MCP3423 V1.0" module at "IIC2" of "NANO_EXP V1.0". | |
// | |
// Required Library : | |
// https://github.com/uChip/MCP342X | |
// | |
// | |
// IIC Address of MCP3423 | |
// | |
const int ADC_MCP_ADDRESS = 0x6E; | |
// | |
// Resistance value of divider and shunt. unit is ohm. | |
// | |
const double RVH = 51.0 * 1000; // Resistance Divider | |
const double RVL = 1.1 * 1000; // Resistance Divider | |
const double RI = 0.005; // Shunt Resistance | |
// | |
// Current value needs to be corrected | |
// | |
const double CurrentCorrection = 1.83333; // current = CurrentCorrection * [original current value] | |
// | |
// MCP3423 | |
// | |
#include <Wire.h> | |
#include <MCP342X.h> | |
MCP342X AdcMcp (ADC_MCP_ADDRESS); | |
// | |
// Global variables | |
// | |
static int16_t Result1 = 0, Result2 = 0; | |
double Voltage = 0; | |
double Current = 0; | |
unsigned long CurrentTime = 0, PreviousTime = 0; | |
void setup() { | |
Wire.begin(); // join I2C bus | |
TWBR = 12; // 400 kHz (maximum) | |
Serial.begin(9600); // Open serial connection to send info to the host | |
while (!Serial) {} // wait for Serial comms to become ready | |
Serial.println("Starting up"); | |
Serial.println("Testing device connection..."); | |
Serial.println(AdcMcp.testConnection() ? "MCP342X connection successful" : "MCP342X connection failed"); | |
} // End of setup() | |
void loop() { | |
// | |
// Read data from channel 1 for voltage | |
// | |
AdcMcp.configure( MCP342X_MODE_CONTINUOUS | | |
MCP342X_CHANNEL_1 | | |
MCP342X_SIZE_16BIT | | |
MCP342X_GAIN_1X | |
); | |
AdcMcp.startConversion(); | |
AdcMcp.getResult(&Result1); | |
// | |
// From MCP3423 spec. | |
// http://ww1.microchip.com/downloads/en/DeviceDoc/22088b.pdf | |
// | |
// On-Board(MCP3423) Voltage Reference (V REF): | |
// - Accuracy: 2.048V ± 0.05% | |
// | |
// MINIMUM AND MAXIMUM OUTPUT CODES | |
// +--------------------------------------------------------------+ | |
// | Resolution Setting | Data Rate | Minimum Code | Maximum Code | | |
// +--------------------------------------------------------------+ | |
// | 16 | 15 SPS | -32768 | 32767 | | |
// +--------------------------------------------------------------+ | |
// | |
// Calculate voltage | |
// | |
Voltage = (((RVH + RVL) * 2.048) / (RVL * 32767.0)) * Result1; | |
// | |
// Read data from channel 2 for current | |
// | |
AdcMcp.configure( MCP342X_MODE_CONTINUOUS | | |
MCP342X_CHANNEL_2 | | |
MCP342X_SIZE_16BIT | | |
MCP342X_GAIN_1X | |
); | |
AdcMcp.startConversion(); | |
AdcMcp.getResult(&Result2); | |
// | |
// See above table and calculate current | |
// | |
Current = ((2.048 * Result2) / 32767.0) / RI; | |
Current *= CurrentCorrection; | |
if (Current >= -0.02 && Current <= 0.02) Current = 0.0; | |
// | |
// Print voltage and current | |
// | |
Serial.print(Voltage); | |
Serial.print("V,"); | |
Serial.print(Current); | |
Serial.println("A"); | |
delay(500); | |
} // End of loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment