Question about sensors and I2C

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
orchard800
 
Posts: 48
Joined: Fri Apr 17, 2015 9:25 pm

Question about sensors and I2C

Post by orchard800 »

Noob question: If I wanted to break out some sensors in conjunction with a couple of rasp pi motor driver hats, how does that affect the I2C addressing?

What I mean is, if I have multiple i2c controller boards, I have to set their addresses using the soldered jumper settings - because they're sharing the gpio pins, right?. But how does this affect other things that use the 40 pins for, like sensors, which don't have any i2c jumpers?

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

Re: Question about sensors and I2C

Post by adafruit_support_bill »

I2c devices use only the SDA and SCL pis. They can share these with other i2c devices as long as the addresses are different. The rest of the GPIO pins are free to use for other purposes.

User avatar
orchard800
 
Posts: 48
Joined: Fri Apr 17, 2015 9:25 pm

Re: Question about sensors and I2C

Post by orchard800 »

So, (and I think this is where I'm getting mixed-up) can the other pins be shared?

Say I wanted 10 different distance sensors - would I have to use 10 different gpio pins?

PS. I am putting some effort in to researching these dumb questions before asking here :)

User avatar
michaelmeissner
 
Posts: 1821
Joined: Wed Aug 29, 2012 12:40 am

Re: Question about sensors and I2C

Post by michaelmeissner »

Input pins can be either digital inputs or analog inputs. I believe the distance sensors need analog inputs, and not digital inputs.

The I2C bus uses 2 dedicated pins SCA/SCL (Gpio 0/1 in the original Pi, Gpio 2/3 in current Pis). When you put different devices on the i2c bus, each device has a unique address. When the i2c master (the Pi) sends out a request, it has an address in the packet, and only the device with that address will answer.

All of the other data pins (i.e. not power and ground) are not used by I2C, and it can be used for any other purpose by your program.

I don't recall whether the Pi has any native analog input GPIO's. That means you need 4 ADS1115 (https://www.adafruit.com/products/1085) or ADS1015 (https://www.adafruit.com/products/1083) to use i2c. You would have to wire up the A0/A1/A2/A3 pins so that each of the ADS1115/ADS1015's have a different address.

There is an alternative chip (MCP3008, https://www.adafruit.com/product/856) that is hooked up to the SPI pins 7 or 8, 9, 10, 11. It provides 8 inputs. Pin 7 is the chip select pin, that is held high when you are talking to that chip. There are two chip select pins (7 and 8), so you might be able to use 2 MCP3008's on a Pi, wiring one to gpio 7 and the other to gpio 8.

I've not done either i2c or spi programming on the Pi (I have done some i2c programming in the arduino world), so I know the capability exists, but not much more than what is in this post.

Here is a tutorial on i2c (for Arduino style microprocessors, but it should explain what is happening on the bus): http://www.gammon.com.au/i2c.

Here is a tutorial on SPI (for Arduino style microprocessors): http://www.gammon.com.au/spi

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

Re: Question about sensors and I2C

Post by adafruit_support_bill »

can the other pins be shared?
Some of them. SPI is another type of serial device bus that allows devices to share some pins. It is a little different from i2c. With SPI, 3 of the pins are shared (MOSI, MISO and SCLK). But each device also needs a unique "CS" (Chip Select) pin. The CS pins cannot be shared.
Say I wanted 10 different distance sensors - would I have to use 10 different gpio pins?
There are several types of distance sensors - with different interface requirements. The Sharp IR Rangefinders need a dedicated analog pin. The Maxbotix ultrasonic rangefinders can work in analog mode, as well as in pulse width or serial. mode. Pulse-width or serial modes would need a digital pin.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”