MCP23008 analog input

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
marplac81
 
Posts: 10
Joined: Fri Apr 14, 2017 5:19 pm

MCP23008 analog input

Post by marplac81 »

I have tried to get 8 bit input from an MCP23008 to an ESP32, and I have searched for Arduino examples and but no luck. I got the Adafruit MCP23008 button and blink examples to work ok.

I modified the Adafruit MCP23008 example for blinking an LED, and left the blink code in to verify communication with the MCP23008. I connected the center pin of a 10k pot to the MCP23008 GP2, (pin 2, physical 12) and its other pins to 3.3V and ground, so I can vary the voltage on pin 2.

On github i found a library file by Limor Fried/Ladyada github page that says:

Code: Select all

/**
 * Read a single port, A or B, and return its current 8 bit value.
 * Parameter b should be 0 for GPIOA, and 1 for GPIOB.
 */
uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) {...
So I thought readGPIO(pin) could work, though the above is for MCP23017, but all I get from pin 2 on the serial monitor is 4's. And I don't know how the parameter b would apply. What am I missing?

BTW, Bing Chat wrote this when I asked it how to get analog data from an MCP23008:

Code: Select all

#include <Wire.h>
#include <Adafruit_MCP23008.h>

Adafruit_MCP23008 mcp;

void setup() {
  mcp.begin(); // use default address 0
}

void loop() {
  uint8_t data = mcp.readGPIO();
  // process the data
}
Here is my code that only outputs
4
4
4
...

Code: Select all

#include <Adafruit_MCP23X08.h>
#include <Wire.h>
#define LED_PIN 0    

Adafruit_MCP23X08 mcp;

void setup() {
    Serial.begin(9600); 
    delay(500);
    Serial.println("MCP23008 Blink and readGPIO Test!");
    if (!mcp.begin_I2C()) {
        delay(500);
        Serial.println("Error.");
        delay(500);
    }
    mcp.pinMode(LED_PIN, OUTPUT);
    mcp.pinMode(2, INPUT);
}

void loop() {
    mcp.digitalWrite(LED_PIN, HIGH);
    delay(1000);
    mcp.digitalWrite(LED_PIN, LOW);
    delay(1000);
    int pin2 = mcp.readGPIO(2);
    Serial.println(pin2);
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23008 analog input

Post by adafruit_support_bill »

The MCP23008 is not an ADC. There is no way to get analog input from it.

What you probably want is the MCP3008 ADC: https://www.adafruit.com/product/856

https://learn.adafruit.com/mcp3008-spi-adc

User avatar
marplac81
 
Posts: 10
Joined: Fri Apr 14, 2017 5:19 pm

Re: MCP23008 analog input

Post by marplac81 »

Thanks, I'll check that out.

User avatar
marplac81
 
Posts: 10
Joined: Fri Apr 14, 2017 5:19 pm

Re: MCP23008 analog input

Post by marplac81 »

What I really need is a device (x8) that both has an addressable ADC input and 2 addressable GPIO outputs for relays to remotely control up to 8 solar window coverings from one ESP32 while sensing the covering positions using a 10 turn pot. Or more than one device at each window but sharing an SPI or I2C cable to avoid massive cabling to all the windows. It may be that I need to use 8 cheap microcontrollers as slaves but I haven't gone that far yet. Nor have I looked at other bus architectures yet. In searching for that one device I haven't succeeded.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23008 analog input

Post by adafruit_support_bill »

What I really need is a device (x8) that both has an addressable ADC input and 2 addressable GPIO outputs
How about a small, inexpensive microcontroller like an Itsy Bitsy? : https://www.adafruit.com/product/3675
It has both analog and digital pins and can be configured to operate as an SPI or i2c slave device.
sharing an SPI or I2C cable to avoid massive cabling to all the windows.
What are the distances involved? i2c was designed for short distances. But there are some tricks to get it to run over longer distances.

User avatar
marplac81
 
Posts: 10
Joined: Fri Apr 14, 2017 5:19 pm

Re: MCP23008 analog input

Post by marplac81 »

Up to about 75ft.

User avatar
marplac81
 
Posts: 10
Joined: Fri Apr 14, 2017 5:19 pm

Re: MCP23008 analog input

Post by marplac81 »

A slave microcontroller would eliminate the Dallas temperature wire needed in the cable that I get window temperatures to allow seasonal control of heat flow in and out. I initially planned using ESP32's at each window to do all this, but it would be overkill and a lot of complexity, with wifi sketch updates etc. Slaves could be simpler.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23008 analog input

Post by adafruit_support_bill »

We have an LTC4311 'active terminator' board for i2c. We've had runs up to 100 feet with that - with a BME680 sensor and Cat-6 cable.
https://www.adafruit.com/product/4756

But it would be a good idea to do a test before pulling cable and installing everything. Drive capability varies a bit between devices and we have seen some that can only do about 2/3 that distance. The GPIO pins on the 32U4 are pretty robust, so I'd expect it to do well. But you probably want to confirm that before installing.

User avatar
marplac81
 
Posts: 10
Joined: Fri Apr 14, 2017 5:19 pm

Re: MCP23008 analog input

Post by marplac81 »

Thanks Bill! I am ordering the itsy and ltc4311 from Adafruit now to get started testing at different lengths.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”