Custom I2C Slave Sensor/device With Arduino

The Idea

In this Instructable, I’ll show you how I managed to create my own I2C controlled device that I can then connect to any microcontroller and interact with it over I2C.

In my case, I’m using it to control an AC dimmer module from a separate Arduino Nano and I then interact with it from another Arduino Nano. You can check the details for controlling the dimmer module in its own Instructable.

This post is sponsored by PCBWay. Get your custom PCBs, SMD stencils, or assembly services professionally done for cheap in less than 24 hours. PCBWay also offers sponsorships for students and hobbyists where you can get your projects built for free.

Supplies

Tools and materials used in the video:

Step 1: The Idea

The IdeaI’m currently working on a bigger project where I need multiple interrupt pins for various sensors and inputs so I was faced with the issue that I ran out of pins.

The dimmer module that I’m using, needs an interrupt pin for the zero-crossing signal that it generates from the mains AC and it then controls the on time for the triac based on that signal. This is quite some work that the controller needs to do, so in order to lighten up the workload on the main controller, I had the idea to separate the AC control to a different Arduino and have the main controller interact with it via I2C.

This way, the main Arduino will only need to deal with the dimmer once to send out the output value and can do the rest of the monitoring in the meantime, while the second Arduino monitors and controls the fan.

Since this is a relatively simple device I originally intended to use an Attiny85 controller for the dimmer controller but I was unable to do so since the I2C pins on the Attiny85 are the same as with the only available interrupt pin.

I read online about a possible solution for using software I2C but I was unable to complete it. If you have any ideas or pointers on how I can achieve this, be sure to leave me a comment.

Instead, I used another Arduino Nano and that worked without issues since the Nano has the I2C lines on pins A4 and A5 while the interrupt is on pin 2.

Step 2: Wiring It Up

Wiring It Up (3)The connections to the AC dimmer module are really simple where besides VCC and GND we need to connect two more pins to the Arduino. In my case, I have the zero-crossing signal coming into pin 2 as this needs to be connected to an interrupt pin and the PWM pin of the module is connected to pin 5 on the Arduino.

The AC line is connected to the input terminal of the module while the light bulb is connected to the load terminal.

Since this is mains voltage, be extra careful with it as any mistakes can seriously injure or even kill you.

To connect the slave Arduino to the master, we need to connect the I2C lines of both Arduinos together. In the case of the Arduino Nano, the pins are A4 and A5 and they need to be connected on both boards together.

Step 3: Code

Code 1 (4)The code used on the two Arduino boards is attached to this Instructable.

In a nutshell, the slave Arduino listens for incoming data on a specific I2C address and when a new value is received, it sends that value to the dimmer library to specify the intensity at which the load is on.

The sole purpose of the master Arduino is to wait for input from the serial monitor on the Arduino IDE and it then relays that information to the slave Arduino at the same address where it listens.

Step 4: Next Steps

Next StepsThe example should give you a clear understanding of how I2C slave mode works and from it, you can then go and build your own custom devices and sensors that can then be integrated with more complicated projects.

If you do not need interrupt pins for the sensor/device that you have in mind, the same can be achieved with the Attiny85 microcontroller and the entire setup will be a lot smaller and manageable.

Read more: Custom I2C Slave Sensor/device With Arduino

Leave a Comment

Your email address will not be published. Required fields are marked *