ESP-01/ESP8266 based multi-channel A/D Converter
In this article I will try to explain how to make a multichannel analog to digital converter with ADS1015 and well-known IoT modules ESP-01 and ESP8266. ADS1015 is a 12 bit I2C-compatible A/D converter which performs conversions at data rates up to 3300 samples per second. It has a build-in configurable programmable gain amplifier (PGA). The PGA offers input ranges from ±256 mV to ±6.144 V, allowing precise large- and small-signal measurements. It can be configured to measure four single-ended or two differential inputs. There are ready to use libraries for these precision converters which speeds up coding on Arduino IDE environment.
ADS1015 have one address pin, ADDR, that configures the I2C address of the device. This pin can be
connected to GND, VDD, SDA, or SCL, allowing for four different addresses to be selected with one pin.
ADDR pin Connection | Slave Address |
GND | 0x48 |
VDD | 0x49 |
SDA | 0x4A |
SCL | 0x4B |
A sample connection diagram where two ADS1015 converters are connected are shown below. With this connection eight single-ended or four differential inputs can be obtained.
Below is a sample Arduino code for reading four differential channels with two ADS1015 converters and displaying results on a web server created by the ESP WiFi module.
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <Wire.h> #include <Adafruit_ADS1015.h> Adafruit_ADS1015 ads; Adafruit_ADS1015 ads2(0x49); ESP8266WebServer server(80); // 80 is the port number const char* ssid = "YOUR SSID"; const char* password = "YOUR PASSWORD"; String htmlPage; String data0, data1, data2, data3, XML; unsigned long int t; void handleRoot() { server.send(200, "text/html", htmlPage); } void XMLcontent() { XML = "<?xml version='1.0'?>"; XML += "<data>"; XML += "<data0>"; XML += data0; XML += "</data0>"; XML += "<data1>"; XML += data1; XML += "</data1>"; XML += "<data2>"; XML += data2; XML += "</data2>"; XML += "<data3>"; XML += data3; XML += "</data3>"; XML += "</data>"; server.send(200, "text/xml", XML); } void setup() { htmlPage = "<html> <style> table, td { padding: 5px; font-size: xxx-large; font-family: Arial, Helvetica, sans-serif; } table "; htmlPage +="{ border-spacing: 15px; } </style> <body onload='process()'> <table \"> <tr > <td>ADC0:</td> <td><div id='div0'>1</div>"; htmlPage +="</td> </tr> <tr> <td>ADC1:</td> <td><div id='div1'>1</div></td> </tr> <tr> <td>ADC2:</td> <td><div id='div2'>1</div></td>"; htmlPage +="</tr> <tr> <td>ADC3:</td> <td><div id='div3'>1</div></td> </tr> </table>"; htmlPage +="</body> </html> <SCRIPT> var xmlHttp = createXmlHttpObject();"; htmlPage +="function createXmlHttpObject() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }"; htmlPage +=" else { xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');"; htmlPage +="} return xmlHttp; } function response() { xmlResponse = xmlHttp.responseXML; for (var i = 0; i < 4; i++)"; htmlPage +=" { xmldoc = xmlResponse.getElementsByTagName('data'+i);"; htmlPage +="message = xmldoc[0].firstChild.nodeValue; document.getElementById('div'+i).innerHTML = message; }"; htmlPage +=" } function process() { xmlHttp.open('PUT', 'xml', true);"; htmlPage +="xmlHttp.onreadystatechange = response; xmlHttp.send(null); setTimeout('process()', 200); } </SCRIPT>"; Serial.begin(9600); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); server.on("/", handleRoot); server.on("/xml", XMLcontent); server.begin(); Serial.print("Readings will be displayed on "); Serial.println(WiFi.localIP()); //Wire.begin(0,2); use this line for ESP-01 Wire.begin(); //use this line for ESP-8266 ads.begin(); ads2.begin(); t = millis(); } void loop() { server.handleClient(); double adc0, adc1, adc2, adc3; double res=3e-3; //default resolution is 3mV if ((millis() - t) > 100) { t = millis(); adc0 = ads.readADC_Differential_0_1()*res; adc1 = ads.readADC_Differential_2_3()*res; adc2 = ads2.readADC_Differential_0_1()*res; adc3 = ads2.readADC_Differential_2_3()*res; data0 = String(adc0); data1 = String(adc1); data2 = String(adc2); data3 = String(adc3); } } |
HTML code used in the code is given below:
<html> <style> table, td { padding: 5px; font-size: xxx-large; font-family: Arial, Helvetica, sans-serif; } table { border-spacing: 15px; } </style> <body onload='process()'> <table "> <tr > <td>ADC0:</td> <td><div id='div0'>1</div></td> </tr> <tr> <td>ADC1:</td> <td><div id='div1'>1</div></td> </tr> <tr> <td>ADC2:</td> <td><div id='div2'>1</div></td> </tr> <tr> <td>ADC3:</td> <td><div id='div3'>1</div></td> </tr> </table> </body> </html> <SCRIPT> var xmlHttp = createXmlHttpObject(); function createXmlHttpObject() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); } return xmlHttp; } function response() { xmlResponse = xmlHttp.responseXML; for (var i = 0; i < 4; i++) { xmldoc = xmlResponse.getElementsByTagName('data' + i); message = xmldoc[0].firstChild.nodeValue; document.getElementById('div' + i).innerHTML = message; } } function process() { xmlHttp.open('PUT', 'xml', true); xmlHttp.onreadystatechange = response; xmlHttp.send(null); setTimeout('process()', 200); } </SCRIPT> |
Web page displayed: