Which i2c device would be easiest for a rookie to experiment with on the PIC  - Page 1
Forum section: electronics / general

author: guitchess
author_rank: Regular Contributor
timestamp: March 09, 2022, 03:38:14 am
content: I've been wanting to learn to use the i2c mode of some of my PIC MCUs. I have previously only tried one device, the RDA5807 FM radio receiver. After getting an Oscope, I was able to see that my rudimentary code was activating the PICs i2c module and sending the address. I have never gotten a response from the receiver chip, though. Not so much as an ACK bit. I thought that if I started with a simpler device, it may reduce the amount of variables.Another option I considered was to use a second PIC as a slave. I have discounted that option as it would be cumbersome to implement. Will reconsider if that is the suggested route.Regardless, the recommendations for simplest to implement i2c devices would be greatly appreciated.Thanks.

author: brucehoult
author_rank: Super Contributor
timestamp: March 09, 2022, 05:48:09 am
content: Maybe something like a temperature or atmospheric pressure/altimeter sensor?

author: JPortici
author_rank: Super Contributor
timestamp: March 09, 2022, 06:00:04 am
content: any i2c eeprom?

author: srb1954
author_rank: Super Contributor
timestamp: March 09, 2022, 06:07:00 am
content: Quote from: guitchess on March 09, 2022, 03:38:14 amTry a simple I/O expander such as the PCF8574. No complicated set-up of internal registers in the chip is required and you can put LEDs on the outputs to verify the data transmitted.

author: woofy
author_rank: Frequent Contributor
timestamp: March 09, 2022, 10:06:25 am
content: Yes, an I/O expander like the PCF8574. It's simple to use and with some LED's will give immediate feedback when you get it correct.The MCP23008 is similar and a bit cheaper.

author: GromBeestje
author_rank: Frequent Contributor
timestamp: March 09, 2022, 10:24:47 am
content: A temperature sensor LM75B, should be straight forwards to use.A little more work, but still quite simple would be an ADXL345 accelerometer.When you are saying, not getting an ACK bit, keep in mind, some manufacturers use 8 bit address notation, others use 7 bit address notation.This is both for I²C slaves as well as the microcontroller (and the APIs their libraries use)So when not getting an ACK, verify the address. Try shifting the address one bit to the left or right.

author: jpanhalt
author_rank: Super Contributor
timestamp: March 09, 2022, 10:35:45 am
content: My first I2C project with a PIC was a DS3231 real-time clock. Very inexpensive modules and accurate.

author: Ian.M
author_rank: Super Contributor
timestamp: March 09, 2022, 12:23:20 pm
content: You cant beat a PCF8574 or PCF8574A eight bit I/O expander for simplicity. All it has is an output register connected to open drain pins with weak pullups, and an input register connected to the same pins. There isn't even a data direction register. What you send to it after the I2C address byte goes straight to its pins. Therefore its ideal for debugging low level I2C transfer code, as there is virtually no chance of confusion about a higher level protocol preventing success.OTOH the MCP23008, although also an 8 bit I/O expander, is far more complex, with eleven registers, and several of them have to be addressed and written to just to get a simple data byte to appear on its pins.

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 09, 2022, 03:49:31 pm
content: Edit: Oops, already said by Ian.M. Well, +1 to that, that's the easiest way to see if your i2c code is working!A cheap GPIO expander with some buttons/LEDs connected would easily let you see the action in real time.Ex. PortA=input, portB=output, then poll PortA and write the value to PortB.The MCU would be doing i2c reads and writes all the time. Pushing the buttons should change the leds if properly working.

author: rstofer
author_rank: Super Contributor
timestamp: March 09, 2022, 05:36:03 pm
content: Find a suitable project on Google and copy/paste. I2C is a difficult protocol to get running so a complete project, even with a different peripheral, is a good learning experience.Here's a project using 2 16F877s as master and slave. It's a start...https://www.teachmemicro.com/pic16-i2c/Hint: Given a choice between SPI and I2C, I always pick SPI. SPI is a lot easier!

author: guitchess
author_rank: Regular Contributor
timestamp: March 09, 2022, 05:48:03 pm
content: Wow! Thank you all so much. Great suggestions and practical devices as well. I will be looking at those asap.

author: hans
author_rank: Super Contributor
timestamp: March 09, 2022, 06:14:09 pm
content: I2C EEPROMs can also be relatively simple to implement. They don't require initialization or setup at all. They also allow you to test read/write parts of I2C. However, any of sensor will typically do as well, as you need to send a command to start the conversion, and then issue a command to read the results (and then issue a I2C restart to read the data, typically).GPIO expander chips or I2C displays probably never need to issue an I2C read, so although they are another easy starting point (you can check the output bits changing on the oscilloscope).. it tests only half of it.If you're not getting a response: check the 7/8-bit address of the device. Make sure that the R/nW LSB bit is set correctly (or just try both to see what happens). Some devices won't ACK a read if it had not been given a command before it.If I'm feeling lazy, I try to find timing diagrams for I2C devices in the datasheets. Often they show the full 8-bit device address for read and write transactions. You can then probably use them as-is in your code.

author: Ian.M
author_rank: Super Contributor
timestamp: March 09, 2022, 06:40:11 pm
content: Quote from: hans on March 09, 2022, 06:14:09 pmOn the contrary. A PCF8574 eight bit I/O expander is the easiest I2C device there is to read from. It powers up with all I/O pins high, weak-pulluped to Vcc and all you need to do is add an 8 way DIP switch to ground selected inputs, or jumpers on a solderless breadboard, and all you need to do to read from it is Start, send its address with the R/W bit set, receive an Ack bit from it, then clock in data bytes, sending an Ack for each. Of course you do need some way of observing the received data, e.g. a hardware debugger, a UART hooked to your PC you can print to, LEDs on other pins of your MCU, etc.I agree that I2C EEPROMs are fairly easy to get going, provided you can pop it in a programmer to pre-load data for your code to read back, or check if your code has successfully written data to it. If not, and without a scope or logic analyser capable of I2C protocol decoding they can be a total bitch. Way back in the days before mass-market internet, when paper databooks were still king, I had to add Vcc switching so I could power down the EEPROM to recover from some states I could get one in while attempting to non-destructively autodetect its size, as simply clocking SCL until it released SDA wasn't working.

author: hans
author_rank: Super Contributor
timestamp: March 09, 2022, 10:30:12 pm
content: You're right. I was thinking about it as an output only device, as I've often used them, and also in the mind of testing.Nonetheless, the use-case I had in mind was to just write the direction register to set pins to output, and then writing an 8-bit upcounting number to the GPIO output port register. Should make blinkies with 8 frequencies..I typically test EEPROMs by writing some data, and then reading it back after some delay (page write time).If the writes stay within 1 page (typ 16 bytes) there is very little in terms of protocol etc. to account for.I agree that debugging protocols without some kind of o'scope or LA is a total PITA. I remember the troubles as a broke student with only a PICKIT2 (and it's 3-ch 1MSPS LA).. today we're blessed with 300$ scopes that do I2C decode, triggering, etc

author: CJay
author_rank: Super Contributor
timestamp: March 10, 2022, 10:18:36 am
content: I2C EEPROM as someone else suggested, there's also the Dallas RTC chips which are fun and useful.GPIO chips like the PCF8574 are also good but the MCP23017 datasheet is written in the same style and language as the PIC chips so it may be a little easier to get to grips with if you're used to those?

author: Doctorandus_P
author_rank: Super Contributor
timestamp: March 10, 2022, 02:23:20 pm
content: Start with buying one of those USD10 logic analysers from Ali / Ebay / china outlet. (Saleaeae Clone) with a Cypress C&CC68013A (All those cheap boxes have that chip) and use it with Sigrok / Pulseview.When writing uC software, I make more use of my EUR 10 Logic analyser, then I use my EUR 300 Rigol oscilloscope.You do need an oscilloscope to verify that the pullups on the I2C bus are strong enough for the bus capacitance.by measuring the RC time constant, and comparing it with the other timing.Alternatively, yo can use relatively strong pullup resistors on SDA and SCL such as 2k2.An I2C bus always needs these resistors. ALWAYS!Sigrok has a lot of extra funcionality. It can tell you a lot of what is going on on the I2C bus and flag errors. It also has more sophisticated protocol decoders for a lot of chips, so it knows (and can tell you) what the data going over the I2C bus is supposed to do with your chip.

author: cv007
author_rank: Frequent Contributor
timestamp: March 10, 2022, 03:05:09 pm
content: QuoteMost likely the address is wrong, assuming the code is at least generating the start/address correctly (and the rda is powered up, and the lines have pullups, ...).You have a scope, so look what the start/address frame actually looks like. If you are not seeing 001000xx for an address (rda uses 0x10 or 0x11, the latter allows 'random' register access), then you and your code have different ideas about what an address is. Ideally, the 7bit address is used and the lower level functions sort out the rw bit and any required shifting, but there are other ideas that float around. Easy enough to figure out what your code needs for an address, but scope will also show what you are getting.If it is that simple, then you may be able to get further with what you already have, but the rda is not a very good beginner i2c ic mainly because of its documentation. In any case, the troubleshooting will not be wasted time as you may have to do it again with any new i2c ic you get (if addresses not correct, you could be in the same boat again), and if you can communicate with the rda your problem then mostly becomes dealing with the rda, not i2c.

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 10, 2022, 05:17:46 pm
content: I'd avoid the scope for decoding unless it's very good.Usually the cheap 24MHz analyzer clone does a much better job, plus you can add additional decoders over the i2c frames, ex. a 24cXX eeprom decoder on top of the i2c decoder.

author: Picuino
author_rank: Super Contributor
timestamp: March 10, 2022, 06:08:37 pm
content: If you use an Arduino in the other side, you can do sniffer tasks, with more control and visualization of I2C communication.

author: Watth
author_rank: Frequent Contributor
timestamp: March 11, 2022, 12:57:47 pm
content: Maybe a LED driver or I/O extender with LEDS and tact switches, so that you can directly see if it works as intended.

author: rstofer
author_rank: Super Contributor
timestamp: March 11, 2022, 07:23:43 pm
content: Not especially relevant to this conversation but for the Raspberry Pi, 'i2c-tools' is installed with Raspian by default.'i2cdetect -y 1' prints a map of the entire address space indicating which devices addresses are in use.https://i2c.wiki.kernel.org/index.php/I2C_ToolsIt runs on all versions of Linux but I suspect there may be driver issues to resolve. I have only used it on a Pi and it works well.https://www.waveshare.com/wiki/Raspberry_Pi_Tutorial_Series:_I2C

author: rstofer
author_rank: Super Contributor
timestamp: March 11, 2022, 07:34:45 pm
content: This may be the long explanation but it has all of the details including C code:https://deepbluembedded.com/i2c-communication-protocol-tutorial-pic/

author: Doctorandus_P
author_rank: Super Contributor
timestamp: March 15, 2022, 01:37:01 pm
content: HT16k33 is a nice chip to start with I2C experiments.It is a combined multiplexed LED driver and keyboard scanner, and it is widely available on different breakout boards with various 7-segment and star LED displays.https://nl.aliexpress.com/wholesale?SearchText=HT16k33ADS1115 (clone?) is another possibility It's a (supposedly) 16-bit ADC.https://nl.aliexpress.com/wholesale?SearchText=ads1115The most important thing though, is to find a chip in an area you are interested in.I2C is a bus system, so you can combine the ADS1115 with theHT16k33 to make a measurement device.Measurement devices are always fun to play with, and this could be good enough to become a permanent part of your electronics kit.Power it from an 18650 (for the power hungry LED display) and put it in a box, and you've got a quite decent start to measure and display voltages.Just add some sensor that outputs a voltage and some software.One Idea is to add a power resistor and MOS fet switch, to measure really low ohm resistors.For example, if you take a 3.9 Ohm power resistor, and put it in series with the (very low ohm) resistor you want to measure, then you have an approximate 1A throug your resistor.Your microcontroller can then calculate the resistor value from measuring the battery voltage, and the voltage over your unknown resistor.To save power, you can use the MOS Fet to turn off the power source most of the time. Just turn the MOS fet on for a few milli seconds. Just long enough to do your measurements.Later you can extend the design.Some ideas:* Do multiple measurements, compare them with each other to get a more reliable result, and get insight in the switching behavior, voltage drop of the 19650 etc.* Also use a 3.9 Ohm and 39 Ohm resistor, for 100mA and 10mA measurement current.* Add a mode with a constant current, so you can measure the DC resistance of Inductors.*

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 15, 2022, 06:16:35 pm
content: Quote from: Doctorandus_P on March 15, 2022, 01:37:01 pmI've used it to drive 14-segment LED displays. It's nice, but it's not without flaws. In particular, it's not very tolerant of errors in the I2C stream, and tends to "lock-up" in this case (preventing further communication until you cycle the power). So, not very friendly for beginners. Maybe I was just unlucky and this was with the particular silicon revision I was using, but I would in any case keep that in mind when using this chip.

author: guitchess
author_rank: Regular Contributor
timestamp: March 16, 2022, 05:43:47 pm
content: Thank you all for your great input. Great responses, as always. You're always a help to an old carpenter pretending to be an EE.I ended up going with the PCF8574 expansion board. Mainly because they're cheap, have lots of docs/howtos, and it was easy to set up for blinking LEDs for instant feedback.I did still have significant trouble getting it running, though. Long story short, the wait routines I had tried weren't working. Ended up randomly trying "while(SSP1CON2bits.SEN ==1) and it worked. Have tried several from the web and it's the only one that works. I guess I'm showing my lack of skills.Now, I'm studying and preparing to try the receiver module again, as soon as I remember where I "stored" it.Thanks again

author: schmitt trigger
author_rank: Super Contributor
timestamp: March 16, 2022, 05:58:30 pm
content: This is an obvious suggestion, as you become proficient and start addressing two or more slaves on the same bus, make sure that you are using the correct I2C speed mode that it is compatible with the slowest device in the bus.Nowadays most if not all modern I2C devices are at least 400 khz compatible. But there are several popular chips that only run at 100 khz. The DS1307 real time clock comes to my mind, but there are plenty others.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: March 16, 2022, 06:22:26 pm
content: Quote from: guitchess on March 16, 2022, 05:43:47 pmstuff like that is why I suggested to buy an EUR 10 Logic Analyzer earlier.

author: guitchess
author_rank: Regular Contributor
timestamp: March 17, 2022, 03:34:29 am
content: Quote from: Doctorandus_P on March 16, 2022, 06:22:26 pmGot one in the cart, just haven't pulled the trigger. It looks like a bargain.Thanks for the advice.

author: Watth
author_rank: Frequent Contributor
timestamp: March 17, 2022, 02:28:38 pm
content: Quote from: guitchess on March 17, 2022, 03:34:29 amI recently bought one, it's very useful!