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:
- Arduino Nano – https://s.click.aliexpress.com/e/_Aey8wk
- AC Dimmer Module – https://s.click.aliexpress.com/e/_A2DIQ4
- Mini Breadboards – https://s.click.aliexpress.com/e/_A69aO4
- Jumper Wires – https://s.click.aliexpress.com/e/_AFkya8
Step 1: The Idea
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
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
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
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