Posts with «i2c» label

Tutorial – TCA9548A 1-to-8 I2C Multiplexer Breakout with Arduino

Now and again you may find yourself needing to use more than one device with the same I2C bus address with your Arduino.

Such as four OLEDs for a large display – or seven temperature sensors that are wired across a chicken hatchling coop. 

These types of problems can be solved with the TCA9548A 1-to-8 I2C Multiplexer Breakout, and in this guide we’ll run through the how to make it happen with some example devices. 

Getting Started

First, consider the TCA9548A itself. It is the gateway between your Arduino and eight separate I2C buses. You have a single bus on one side, connected to your Arduino.

On the other side of the TCA9548A, you have eight I2C buses, and only one of these can be connected to the Arduino at a time. For example (from the data sheet):

The TCA9548 can operate on voltages between 1.8 and 5V DC… and operate with devices that have operating voltages between 1.8 and 5V DC. This is very convenient, as (for example) you can use devices made for 3.3V operation with 5V Arduinos, or vice versa. Awesome. So let’s get started. 

The breakout board includes inline header pins, which are not soldered to the board. So you need to do that. An easy way to line up the pins properly is to drop them into a solderless breadboard, as such:

Then after a moment or two of soldering, you’re ready to use:

Next, insert your module into a solderless breadboard and wire it up as shown:

We are using the red and blue vertical strips on the breadboard as 5V and GND respectively. Finally, we connect the 5V and GND from the Arduino to the solderless breadboard, and A4/A5 to SDA/SCL respectively on the breakout board:

The electrical connections are as follows (Module — Arduino):

  • Vin to 5V
  • GND to GND
  • A0 to GND
  • A1 to GND
  • A2 to GND
  • SDA to A4
  • SCL to A5

Next, we consider the I2C bus address for the TCA9548A. Using the wiring setup shown above, the address is set to 0x70. You only need to change this if one of your other devices also has an address of 0x70, as shown in the next step.

Changing the I2C address of the TCA9548A

The bus address of the TCA9548A is changed using the connections to the A0, A1 and A2 pins. By default in the tutorial we use 0x70, by wiring A0~A2 to GND (known as LOW). Using the table below, you can reconfigure to an address between 0x70 and 0x77 by matching the inputs to HIGH (5V) or LOW (GND):

Testing 

Before we get too excited, now is a good time to test our wiring to ensure the Arduino can communicate with the TCA9548A. We’ll do this by running an I2C scanner sketch, which returns the bus address of a connected device. 

Copy and paste this sketch into your Arduino IDE and upload it to your board.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning…");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
delay(5000);
}
view raw i2cscanner.ino hosted with ❤ by GitHub

Then, open the serial monitor and set the data rate to 115200. You should be presented with something like the following:

As you can see, our scanner returned an address of 0x70, which matches the wiring described in the bus address table mentioned earlier. If you did not find success, unplug the Arduino from the computer and double-check your wiring – then try again.

Controlling the bus selector

Using the TCA9548A is your sketch is not complex at all, it only requires one step before using your I2C device as normal. That extra step is to instruct the TCA9548A to use one of the eight buses that it controls. 

To do this, we send a byte of data to the TCA9548A’s bus register which represents which of the eight buses we want to use. Each bit of the byte is used to turn the bus on or off, with the MSB (most significant bit) for bus 7, and the LSB (least significant bit) for bus 0.

For example, if you sent:

0b00000001 (in binary) or 0 in decimal

… this would activate bus zero. 

Or if you sent:

0b00010000 (in binary)

… this would activate bus five. 

Once you select a bus, the TCA9548A channels all data in and out of the bus to the Arduino on the selected bus. You only need to send the bus selection data when you want to change buses. We’ll demonstrate that later. 

So to make life easier, we can use a little function to easily select the required bus:

void TCA9548A(uint8_t bus)
{
  Wire.beginTransmission(0x70);  // TCA9548A address is 0x70
  Wire.write(1 << bus);          // send byte to select bus
  Wire.endTransmission();
}

This function accepts a bus number and places a “1” in the TCA9548A’s bus register matching our requirements. Then, you simply slip this function right before needing to access a device on a particular I2C bus. For example, a device on bus 0: 

TCA9548A(0);

 … or a device on bus 6:

TCA9548A(6);

A quick note about pull-up resistors

You still need to use pull-up resistors on the eight I2C buses eminating from the TCA9548A. If you’re using an assembled module, such as our example devices – they will have the resistors – so don’t panic.

If not, check the data sheets for your devices to determine the appropriate pull-up resistors value. If this information isn’t available, try 10k0 resistors.

Controlling our first device

Our first example device is the tiny 0.49″ OLED display. If you haven’t seen this display before, click here for the tutorial.

It is has four connections, which are wired as follows (OLED — TCA9548A/Arduino):

  • GND to GND
  • Vcc to Arduino 3.3V
  • CL to TCA9548A SC0 (bus #0, clock pin)
  • DA to TCA9548A SD1 (bus #0, data pin)

The OLED runs from 3.3V, so that’s why we’re powering it directly from the Arduino’s 3.3V pin. 

Now, copy and upload this sketch to your Arduino:

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
void TCA9548A(uint8_t bus)
{
Wire.beginTransmission(0x70); // TCA9548A address is 0x70
Wire.write(1 << bus); // send byte to select bus
Wire.endTransmission();
}
U8G2_SSD1306_64X32_1F_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup()
{
Wire.begin();
u8g2.begin();
}
void loop()
{
TCA9548A(0); // tell the TCA9548A we want to use I2C bus number zero (to talk to the OLED)
// use the OLED as normal
for (int a = 999; a >= 0; –a)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_inb24_mr ); // choose a suitable font
u8g2.setCursor(0, 24);
u8g2.print(a);
a = a – 47;
u8g2.sendBuffer(); // transfer internal memory to the display
delay(100);
}
}

After a moment the OLED will display some numbers counting down in various amounts (video):

So how did that work? We inserted our bus selection function at line 5 of the sketch, then called the function in at line 22 to tell the TCA9548A that we wanted to use I2C bus zero. Then the rest of the sketch used the OLED as normal. 

Controlling two devices

Let’s add another device, a BMP180 barometric pressure sensor module. We’ll connect this to I2C bus number seven on the TCA5948A. There are four connections, which are wired as follows (BMP180 — TCA9548A/Arduino):

  • GND to GND
  • Vcc to Arduino 3.3V
  • CL to TCA9548A SC0 (bus #7, clock pin)
  • DA to TCA9548A SD1 (bus #7, data pin)

Now, copy and upload this sketch to the Arduino, and after a moment the OLED will display the ambient temperature from the BMP180 in whole degrees Celsius.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <Arduino.h>
#include <U8g2lib.h>
#include <Adafruit_BMP085.h>
#include <Wire.h>
U8G2_SSD1306_64X32_1F_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
Adafruit_BMP085 bmp;
int temperature;
void TCA9548A(uint8_t bus)
{
Wire.beginTransmission(0x70); // TCA9548A address is 0x70
Wire.write(1 << bus); // send byte to select bus
Wire.endTransmission();
}
void setup()
{
Wire.begin();
u8g2.begin();
TCA9548A(7); // select I2C bus 7 for the BMP180
if (!bmp.begin())
{
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop()
{
// first, get the temperature from the BMP180
TCA9548A(7); // select I2C bus 7 for the BMP180
temperature = int(bmp.readTemperature());
// next, display the temperature on the OLED
TCA9548A(0); // select I2C bus 0 for the OLED
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_inb24_mr ); // choose a suitable font
u8g2.setCursor(0, 24);
u8g2.print(temperature);
u8g2.sendBuffer();
delay(100);
}
view raw TCA9548A3.ino hosted with ❤ by GitHub

This is demonstrated in the following video (finger is placed on the BMP180 for force a rise in temperature)(video):

So how did that work? We set up the libraries and required code for the OLED, BMP180 and TCA5948A as usual. 

We need to intialise the BMP180, so this is done at line 24 – where we select the I2C bus 7 before initiating the BMP180.

The the sketch operates. On line 34 we again request I2C bus 7 from the TCA9548A, then get the temperature from the BMP180.

On line 38 we request I2C bus 0 from the TCA9548A, and then display the temperature on the OLED. Then repeat.

A quick note about the reset pin

More advanced users will be happy to know they can reset the TCA9548A status, to recover from a bus-fault condition. To do this, simply drop the RESET pin LOW (that is, connect it to GND). 

Where to from here? 

You can now understand through our worked example how easy it is to use the TCA9548A and access eight secondary I2C buses through the one bus from your Arduino. Don’t forget that the TCA9548A also does double-duty as a level converter, thereby increasing its value to you. 

I hope you enjoyed reading about these useful TCA9548A boards. To keep up to date with new posts at tronixstuff.com, please subscribe to the mailing list in the box on the right, or follow us on x – @tronixstuff.

If you find this sort of thing interesting, please consider ordering one or more of my books from amazon:

And, as always, have fun and make something.

Tronixstuff 14 Apr 01:25
arduino  i2c  tca9548a  

A Usable Arduino Debugging Tool

For as popular as the Arduino platform is, it’s not without its problems. Among those is the fact that most practical debugging is often done by placing various print statements throughout the code and watching for them in the serial monitor. There’s not really a great way of placing breakpoints or stepping through code, either. But this project, known as eye2see, hopes to change that by using the i2c bus found in most Arduinos to provide a more robust set of debugging tools.

The eye2see software is set up to run on an Arduino or other compatible microcontroller, called the “probe”, which is connected to the i2c bus on another Arduino whose code needs to be debugged. Code running on this Arduino, which is part of the eye2see library, allows it to send debugging information to the eye2see probe. With a screen, the probe can act as a much more powerful debugger than would otherwise typically be available, being able to keep track of variables in the main program, setting up breakpoints, and outputting various messages on its screen.

The tool is not without its downsides, though. The library that needs to run on the host Arduino slows down the original program significantly. But for more complex programs, the tradeoff with powerful debugging tools may be worth it until these pieces of code can be removed and the program allowed to run unencumbered. If you’d like to skip needing to use a second Arduino, we’ve seen some other tools available for debugging Arduino code that can run straight from a connected PC instead.

Hack a Day 31 Jul 19:30

Digital Replica of Antique Weather Monitoring Instrument

Computers and digital sensors have allowed for the collection and aggregation of data barely possible to imagine to anyone in the instrumentation scene even sixty years ago. Before that, things like weather stations, seismometers, level sensors, and basically any other way of gathering real data about the world would have been performed with an analog device recording the information on some sort of spool of paper. This was much more tedious but the one thing going for these types of devices was their aesthetic. [mircemk] is back to bring some of that design inspiration to a digital barometric display.

The barometer is based around an Arduino Arduino Nano and a relatively large I2C display to display the captured data. It also uses a BME 280 pressure sensor board, but the technical details of this project are not the focal point here. Instead, [mircemk] has put his effort in recreating the old analog barographs, which display barometric data on a spool of paper over time, on the I2C display. As the device measures atmospheric pressure, it adds a bar to the graph, displaying the data over time much as the old analog device would have.

We’ve discussed plenty of times around here that old analog meters and instrumentation like this recreation of a VU meter are an excellent way of getting a more antique aesthetic than is typically offered by digital replacements. Adding in a little bit of style to a project like this can go a long way, or you can simply restore the original antique instead.

Hack a Day 22 Feb 21:00

You Can Send MIDI Over I2C If You Really Need To

The Musical Instrument Digital Interface has a great acronym that is both nice to say and cleanly descriptive. The standard for talking to musical instruments relies on a serial signal at 31250 bps, which makes it easy to transmit using any old microcontroller UART with a settable baud rate. However, [Kevin] has dived into explore the utility of sending MIDI signals over I2C instead.

With a bit of hacking at the Arduino MIDI library, [Kevin] was able to get the microcontroller outputting MIDI data over the I2C interface, and developed a useful generic I2C MIDI transport for the platform. His first tests involved using this technique in concert with Gravity dual UART modules. After he successfully got one running, [Kevin] realised that four could be hooked up to a single Arduino, giving it 8 serial UARTS, or, in another way of thinking, 8 MIDI outputs.

At its greatest level of development, [Kevin] shows off his I2C MIDI chops by getting a single Raspberry Pi Pico delivering MIDI signals to 8 Arduinos, all over I2C. All the Arduinos are daisy-chained with their 5V and I2C lines wired together, and the system basically swaps out traditional MIDI channels for I2C addresses instead.

There’s not a whole lot of obvious killer applications for this, but if you want to send MIDI data to a bunch of microcontrollers, you might find it easier daisy-chaining I2C rather than hopping around with a serial line in the classic MIDI-IN/MIDI-THRU fashion.

We’ve seen [Kevin]’s work before too, like the wonderful Lo-Fi Orchestra. Video after the break.

Hack a Day 16 Feb 06:00

I2C To The Max With ATtiny

The Arudino is a powerful platform for interfacing with the real world, but it isn’t without limits. One of those hard limits, even for the Arduino MEGA, is a finite number of pins that the microcontroller can use to interface with the real world. If you’re looking to extend the platform’s reach in one of your own projects, though, there are a couple of options available. This project from [Bill] shows us one of those options by using the ATtiny85 to offload some of an Arduino’s tasks using I2C.

I2C has been around since the early 80s as a way for microcontrollers to communicate with each other using a minimum of hardware. All that is needed is to connect the I2C pins of the microcontrollers and provide each with power. This project uses an Arduino as the controller and an arbitrary number of smaller ATtiny85 microcontrollers as targets. Communicating with the smaller device allows the Arduino to focus on more processor-intensive tasks while giving the simpler tasks to the ATtiny. It also greatly simplifies wiring for projects that may be distributed across a distance. [Bill] also standardizes the build with a custom dev board for the ATtiny that can also double as a shield for the Arduino, allowing him to easily expand and modify his projects without too much extra soldering.

Using I2C might not be the most novel of innovations, but making it easy to use is certainly a valuable tool to add to the toolbox when limited on GPIO or by other physical constraints. To that end, [Bill] also includes code for an example project that simplifies the setup of one of these devices on the software end as well. If you’re looking for some examples for what to do with I2C, take a look at this thermometer that communicates with I2C or this project which uses multiple sensors daisy-chained together.

Low-Cost Computer Gesture Control with an I2C Sensor

Controlling your computer with a wave of the hand seems like something from science fiction, and for good reason. From Minority Report to Iron Man, we’ve seen plenty of famous actors controlling their high-tech computer systems by wildly gesticulating in the air. Meanwhile, we’re all stuck using keyboards and mice like a bunch of chumps.

But it doesn’t have to be that way. As [Norbert Zare] demonstrates in his latest project, you can actually achieve some fairly impressive gesture control on your computer using a $10 USD PAJ7620U2 sensor. Well not just the sensor, of course. You need some way to convert the output from the I2C-enabled sensor into something your computer will understand, which is where the microcontroller comes in.

Looking through the provided source code, you can see just how easy it is to talk to the PAJ7620U2. With nothing more exotic than a switch case statement, [Norbert] is able to pick up on the gesture flags coming from the sensor. From there, it’s just a matter of using the Arduino Keyboard library to fire off the appropriate keycodes. If you’re looking to recreate this we’d go with a microcontroller that supports native USB, but technically this could be done on pretty much any Arduino. In fact, in this case he’s actually using the ATtiny85-based Digispark.

This actually isn’t the first time we’ve seen somebody use a similar sensor to pull off low-cost gesture control, but so far, none of these projects have really taken off. It seems like it works well enough in the video after the break, but looks can be deceiving. Have any Hackaday readers actually tried to use one of these modules for their day-to-day futuristic computing?

An Arduino And A CD-ROM Drive Makes A CD Player

In an age of streaming media it’s easy to forget the audio CD, but they still remain as a physical format from the days when the “Play” button was not yet the “Pay” button. A CD player may no longer be the prized possession it once was, but it’s still possible to dabble in the world of 120 mm polycarbonate discs if you have a fancy for it. It’s something [Daniel1111] has done with his Arduino CD player, which uses the little microcontroller board to control a CD-ROM drive via its IDE bus.

The project draws heavily from the work of previous experimenters, notably ATAPIDUINO, but it extends them by taking its audio from the drive’s S/PDIF output. A port expander drives the IDE interface, while a Cirrus Logic WM8805 S/PDIF transceiver handles the digital audio and converts it to an I2S stream. That in turn is fed to a Texas Instruments PCM5102 DAC, which provides a line-level audio output. All the code and schematic can be found in a GitHub repository.

To anyone who worked in the CD-ROM business back in the 1990s this project presses quite a few buttons, though perhaps not enough to dig out all those CDs again. It would be interesting to see whether the I2S stream could be lifted from inside the drive directly, or even if the audio data could be received via the IDE bus. If you’d like to know a bit more about I2S , we have an article for you.

Hack a Day 03 Jan 12:00

Big Time Character LCD Clock

While the SSD1306 OLED has somewhat become the go-to display for up-to-date projects, the good old character displays with their Hitachi HD44780 controller don’t seem to be disappearing just yet either. And why would they, especially if you want to show just text, having a built-in font has certainly its perk compared to worrying about integrating your own characters — which you can still do on top as well. Or perhaps you can combine both worlds, which is what [oldmaninSC] did with his digital clock that takes an entire 16×2 LCD to show each single digit.

The whole clock uses 16 individual, upright rotated 16×2 LCDs that are arranged in two rows of eight LCDs each, turning the entire construct sort of into a giant 8×2 display itself. For some additional information such as the date, there’s also a smaller font available that uses only half the height, allowing up to four total rows of information. To communicate with each LCD via I2C, two TCA9548A I2C multiplexers are connected to an Arduino, along with an RTC to keep track of the time and date itself.

As the TCA9548A has three pins dedicated to define its own address, the entire clock could be scaled up to a total of 64 LCDs — so how about a 16×4 display made out of 16×4 displays? Sure, adding smooth scrolling might become a bit tricky at some point, but imagine playing Tetris on that one!

Back To Basics With An Arduino And An EEPROM

There are plenty of techniques and components that we use in our everyday hardware work, for which their connection and coding is almost a done deal. We are familiar with them and have used them before, so we drop them in without a second thought. But what about the first time we used them, we had to learn somewhere, right? [TheMagicSmoke] has produced just what we’d have needed then for one component that’s ubiquitous, the I2C EEPROM.

These chips provide relatively small quantities of non-volatile memory storage, and though they are not the fastest of memory technologies they have a ready application in holding configuration or other often-read and rarely written data.

Since the ST24C04 512-byte device in question has an I2C bus it’s a straightforward add-on for an Arduino Mega, so we’re shown the wiring for which only a couple of pull-down resistors are required, and some sample code. It’s not the most complex of projects, but it succinctly shows what you need to do so that you too can incorporate an EEPROM in your work.

If learning about I2C EEPROMs piques your interest, perhaps you’d like to read a previous look we made at them.

Hack a Day 25 May 12:00
arduino  eeprom  i2c  i2c eeprom  parts  

Clock super-display

Today was a good day. In typical fashion, I started a few new "projects" almost in the same time. First one, it's assembling of a new kind of clock, from a kit sent by Nick S. I got stuck pretty early though, so I "parked" it for now. Details to come soon, in a special post.

Second one, an "Adler 121PD" vintage calculator with a VFD display, that I found "in the dumpster" (well, not really, but the idea is the same, I got it for free). I was going to break it apart, for the display and the circuitry, but I gave up when I powered it up (with an improvised cable; the original, proprietary one, was missing) and it actually worked! I may still go ahead with dis-assembling it, since it is not a great value anyway; I checked prices on ebay, and they go for around $20.

Lastly, the project that gave the name of this post: a clock LED super-display, consisting of 3 individual and independent indicators, inspired by the Leitch studio clock, brought to my attention by Nick (VE2HOT). The goal for the clock super-display is to eventually be able to emulate the Leitch clock. Here it is, in its incipient glory (only the back panel; the black wooden frame not pictured):


Since I am not the crafty kind-of-guy (also not keen on spending for form more than for content), I am always looking for cheap, easy and quick solutions for encasing electronics. In this case, Ikea's Ribba 9"x9" frame ($10) seems to be a good fit for the job, and hopefully will help the future clock look "Leitchy" or even better (Nick's photo below):


The 2 alphanumeric displays (4 and 8 chars) of the clock super-display are I2C-driven. The 60 LED ring is adafruit neopixel, controlled by a single pin. With this setup, even an ESP8266 module could be used as the brains of the clock.

The ring is fixed to the cardboard back/panel of the deep Ikea frame with four M3 plastic standoffs glued to the PCB.
The 4-character alphanumeric 16-segment is my creation, introduced earlier. It is driven by the HT16K33 backpack, also from adafruit (not in the picture). The PCB has M3 holes for screws.
The 8-character alphanumeric is made of two side-by-side quad 14-segment LED displays, also from adafruit. The 2 modules already have the HT16K33 drivers installed (soldered on the back). Attaching these quad displays to the panel is not easy, since the holes are probably M1.4. Even these thin M1.4 screws need to be forced, because the screw head presses against display's plastic enclosure. Eventually, the M1.4 screws will be glued to the M3 plastic standoffs, that's the best I could come up with. It is weird that, for such a popular and successful product, one cannot find photos (or instructions) on mounting these modules using screws.

Next step is the software support in the WiFiChron software. Also need to find a way to access the 3 buttons: having them in the back is not a good idea, having them in the front is impossible, unless the glass is replaced with transparent/smoky/grey acrylic, which can be drilled.

Wise time with Arduino 11 May 02:13
esp8266  hdsp  i2c  wifichron