I'm trying to create a project that will measure the resistance of a resistor I'm using a 10k pulldown resistor after the analog pin also connected to the ground pin. I'm using this code:
int raw = analogRead(0);
Serial.println(raw);
if (raw) {
int buff = raw * 3.3;
int vout = (buff)/1024.0;
buff = (3.3/(vout)) -1;
res = 10000 * buff;
}
When I run this sketch, I get an output of -10000. What am I doing wrong?
Please post your sketch, complete, not just the snippet where you do the calculation (because in that case more often than not the problem is in the part you don't show).
/*****************************************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
//#define WLAN_SSID "Thom"
//#define WLAN_PASS "thom1111"
#define WLAN_SSID "MySpectrumWiFi64-2G"
#define WLAN_PASS "narrowdaisy835"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "MaxMaeder"
#define AIO_KEY "3754a5c50ec647ab99ae315fcfab09c5"
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish waterHardness = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.grains-per-gallon");
Adafruit_MQTT_Publish ppm = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.parts-per-million");
Adafruit_MQTT_Publish conductivity = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.conductivity");
Adafruit_MQTT_Publish alerts = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.alerts");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
int saltLevel = 0;
int dataTransfer = 1;
int flash = 0;
float res = 0;
float conduct = 0;
void MQTT_connect();
void setup() {
digitalWrite(12, HIGH);
Serial.begin(115200);
delay(10);
Serial.println(F("Please wait..."));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print(F(" Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected! "));
Serial.println(F("IP address: ")); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff feed.
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
String wifiAlerts = "";
int newSubscription = 0;
float raw = analogRead(0);
Serial.println(raw);
if (raw) {
float buff = raw * 3.3;
float vout = (buff)/1024.0;
buff = (3.3/(vout)) -1;
res = 10000UL * buff;
conduct = 1/res;
saltLevel= conduct * 2;
saltLevel = saltLevel * 17.1;
Serial.println(res);
}
MQTT_connect();
Serial.println();
Serial.print("Data transfer #");
Serial.println(dataTransfer);
dataTransfer++;
// Now we can publish stuff!
Serial.print(F("Sending Grains Per Gallon Value... "));
if (! waterHardness.publish(saltLevel)) {
Serial.println(F("Failed "));
} else {
Serial.println(F("Sent! "));
}
Serial.print(F("Sending Parts Per Million Value... "));
Serial.print(F("Sending uS/CM Value... "));
if (! conductivity.publish(conduct)) {
Serial.println(F("Failed "));
} else {
Serial.println(F("Sent! "));
}
Serial.print(F("Sending Resistance Value... "));
if (! ppm.publish(res)) {
Serial.println(F("Failed "));
} else {
Serial.println(F("Sent! "));
}
if (saltLevel > 1000) {
wifiAlerts = "Hard Water Alert";
if (flash == 1) {
digitalWrite(0, LOW);
flash = 0;
} else {
digitalWrite(0, HIGH);
flash = 1;
}
} else {
wifiAlerts = "No Alerts To Display";
if (WiFi.status() != WL_CONNECTED) {
if (flash == 1) {
digitalWrite(0, LOW);
flash = 0;
} else {
digitalWrite(0, HIGH);
flash = 1;
}
} else {
digitalWrite(0, HIGH);
}
}
Serial.print(F("Sending Alerts Data ("));
Serial.print(wifiAlerts);
Serial.print(F(")... "));
if (! alerts.publish(wifiAlerts.c_str())) {
Serial.println(F("Failed "));
} else {
Serial.println(F("Sent! "));
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
delay(500);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print(F("Connecting to MQTT... "));
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println(F("Retrying MQTT connection in 5 seconds... "));
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println(F("MQTT Connected! "));
}
So you're using an ESP8266 based Arduino (with 0-1volt analogue input).
The boards with 3.3volt analogue-in use a voltage divider (with resistors) to drop 0-3.3volt to 0-1volt.
That divider will upset your fixed 10k resistor.
Better find out if your board uses a voltage divider.
Leo..
It has a maximum input of one, but no voltage divider (I am using the Adafruit Feather HUZZAH ESP8266). Can I compensate for this in my code, or am I going to have to install a 3.3 to 1-volt converter for the power leading to the resistor I want to find the resistance of?
You seem to be making a meal out of reading an analog pin voltage - why not simply:
#define Vsupply 3.3 // or whatever it needs to be
#define R0 10000.0 // name your constants at top level, don't sprinkle throughout the code
#define RESISTOR_PIN A0
float read_resistor ()
{
float Vpin = analogRead(RESISTOR_PIN) * Vsupply / 1024 ; // one line, simple to read
return R0 * (Vsupply - Vpin) / Vpin ; // simply clear calculation using voltage ratio
}
(Which would work if no other resistors on the pin)
Reading the code I see comments referring to measuring the resistance of water (i.e. an ionic solution). You can't measure this using a DC current, you'll get electrolysis and the measured resistance will change with time.