Introducing the ATtiny Device PCB - I2C slave devices

Learn how you can make DIY I2C devices by using the ATtiny Device PCB and an Attiny85.
Dec 14, 2021 — 9 mins read — Arduino

Introducing the ATtiny Device PCB - I2C slave devices

Ever since I started playing with Arduinos and microcontrollers, I was always fascinated by one very specific chip.

The Attiny85!

Attiny85

Attiny85

I know, I know! It's not the fastest microcontroller out there, nor it is the best but being so small and requiring no external components for it to work is what makes it so interesting to me.

I've used it before, both for projects on the channel where I've made an automatic vacuum switch for my workshop and for personal projects but I never really gave it a better chance for proving itself so today, that is all about to change.

How did we get here?

A while ago, I made a video that was the trigger for all of this. In that video, I made two Arduino Nano boards talk to each other via I2C on a parent/child relationship in an attempt to free the parent a.k.a. main Arduino from certain processing tasks.

The benefit of that approach is that now, the child Arduino can handle a single or multiple repetitive and highly CPU intensive jobs while the main board will be left free to handle the rest of the functionalities and only provide instructions to the child Arduino.

This all worked beautifully, but what I wasn't able to accomplish at that time is to use an ATTiny85 for the child controller, because the I2C communication and the dimmer module required pin 7 (PB2) to be used for different purposes at the same time. That is still an issue, BUT, that experiment sparked the idea for what I now call, ATtiny Device.


To design this board, I used Altium Designer. Altium Designer is your best choice when it comes to PCB design where they provide all the necessary tools for all of your ideas. Schematic capture, high speed, rigid-flex, HDI, they have it all and on top of that, you can collaborate with multiple people at the same time to bring your ideas to life.

A critical feature for the current state of the world that we are in is their Manufacturer Part Search where you can see directly the stock available for the selected parts and base your design decisions on that. This alone can now be a decisive factor if your project can leave the design phase or get stuck due to the shortage of components.

At the moment, Altium provides a free trial to all of my viewers where you can test the software for free so check out here.


What is an ATtiny Device?

So, what is an ATtiny Device? Well, the short answer is that I don't know.

ATtiny Device PCB

ATtiny Device PCB

The board is designed to serve as a carrier platform for projects around the Attiny85 where it directly exposes the power pins and the I2C pins to a header on one side.

What it can be though, is entirely on your imagination. For me, this board can be a mood/status lamp, it can be a light sensor or it can be a controller for an addressable LED strip. Basically, this board can be turned into whatever device that you can think of.

Relay timer control, of course, it can be that as well. In fact, let's build one now so you can see how this tiny board is converted into an actual ATtiny Device.


But, before we jump into soldering, let me tell you about the manufacturer of this board, PCBWay. I've been using PCBWay for a while now to manufacture my prototypes and I can not be happier with them. They have awesome build quality, fast turnaround, and a lot of options to choose from.

Right now, they run their Christmas sale, where you can win a ton of coupons, win some gifts or get your project manufactured for some really low prices.


What we will build?

The device that we will make now is a relay timer. The relay timer will be controlled by I2C so whenever we send a specific character to it, the device will turn on the relay for a set period of time.

The finished relay timer being used over I2C

The finished relay timer being used over I2C

Think of it as a stairway light controller maybe? Different floor sends out a different code and based on that code the light stays on for the different durations.

Or as a controller for some mixing machine that needs a specific amount of water for the recipe that it currently makes. By sending a specific command, the relay will be on for a specific amount of time and deliver the right water volume.

In the meantime, the main board can continue doing whatever it was doing before in a "set and forget" style while the Attiny85 does the needed counting.

Assembling an ATtiny Device

To start assembling what we first need is this board that I designed and for that, you have three options.

Option number one is that you build it yourself from a perf board as I did while testing the design.

Another option is to buy this board from me as I've listed the board for sale on my Tindie shop that I just opened. If you choose to buy it, thank you! That money goes into growing this website and channel where they allow me to make better projects and better videos as well.

The third and final option to get this board is to leave a comment down below the video indicating that you want one and I will select 5 people that will receive the board for free. The giveaway is open until December 28th, 2021.

Next, you will need an 8 pin IC socket, an Attiny85 microcontroller, a 4 pin male header, and a 6 pin female header. Additionally, you will need a relay module and a spare Arduino Uno for programming the ATTiny85.

Here are some links from where you can get all of the required components:

You can get the full written instructions on how to assemble the relay timer on the dedicated Instructable or by watching the video above.

Arduino sketch for the ATtiny Device

What is really important now for our device is to get its brains. In a nutshell, we need code that will turn this into an I2C device that we can then connect to the main project. Since the main project is relatively irrelevant for our demonstration, I'll use an example sketch that allows us to send data over I2C from the serial monitor of the Arduino IDE.

With this sketch, anything that I write inside the serial monitor will then be sent to the ATtiny Device and we can then observe how it changes and behaves. You can get the full sketches used in this example in my GitHub repo.

Now, let's focus on the actual device code. To be able to run this device as an I2C slave device, we need a library called TinyWireS. So we now first include that library into our sketch and we need to initialize it with an address at which we want our device to be discoverable.

This address needs to be in the range of 0 to 127 and it also must be unique for the device for the project. You can not have two devices in the same project that share the same address.

Since this is our only device now, we can choose any address that we want, so I set the address to 13 in the sketch setup function.

void setup()
{
 pinMode(3, OUTPUT );
 TinyWireS.begin(13);         
 TinyWireS.onReceive(receiveEvent);
}

What we also need to set up here is the callback function that will be executed whenever we communicate with this device. This is done by using the "onReceive" method from the library where we specify the name of the function that we want to be executed once communication is detected.

The important thing about this function is that we always need to keep it as short and simple as possible. The I2C protocol relies on a precisely timed communication that we can mess up if we try to put our device logic here.

void receiveEvent(int howMany)
{
 state = TinyWireS.receive();
}

Instead, in this function, we only receive and store the external data and we can then act against it in the main loop function of the sketch.

Now, in the main loop of the program, the very first thing that we need to do is to call a specific function that is defined in the TinyWire library that will make sure to send out the correct stop signal to the I2C communication.

Since the Attiny85 has very limited resources, one of the drawbacks is that in order to have reliable I2C communication, we are not really allowed to use delays in our code. Any waiting that we need to implement we need to get creative and implement it differently.

Since we are building a relay timer, our delay is implemented by using the millis function. This function returns the elapsed time in milliseconds since the Arduino was started.

We can use this value to add a number to it so we can define a point in the future that we want an action to take place.

So whenever we detect that we've received a new state from the main board, we check its value and add different delays for each. For example, if the received value is 1, we add 2 seconds of delay and if the received value is 4 we then add 10 seconds of delay.

Next, we can check the value of this time and if that time is into the future, that means that we now need to turn on the relay and keep it on until we reach that point in time.

Once the current millis surpasses the setpoint, we can then turn off the relay until the next command is received.

if(endMillis <= millis()) {
  digitalWrite(3, HIGH);
 } else {
  digitalWrite(3, LOW);
 }

With the addition of the default case in the switch command where we check the state, we can also send any undefined character as a command to the board to terminate the current timing.

So for example, if we set a timer for 10 seconds by sending 4 to the ATtiny Device, after a second or so, we can then send 0 to reset the timer and turn off the relay immediately.

What's next for the ATtiny Device?

Now, that we have a device that can interact through I2C with Arduino, this can be expanded to all sorts of other projects and boards like the Raspberry Pi for example.

With it, we can build all sorts of sensors for a relatively cheap price as the Attiny85 is also a very cheap microcontroller. Before the current chip shortage that we are in, it could have been bought for price at around 50 cents per piece. It now costs more than that but I'm sure that prices will go down once manufacturing settles in.

To bring this project to the next level, I would like to get your feedback. If you have an idea for a sensor, device, or any specific project where we can use the Attiny85 and turn it into an ATtiny Device, then please let me know in the comments below or on YouTube under the video.

I do have a few of my own so make sure to subscribe to the channel if you want to see them built and I can't wait to hear your ideas for what else we can build with this board.

Take care, and I'll see you in the next one.

i2c attiny PCB relay
Read next

Volume knob replacement on a car stereo

This week I did one more repair and this time, it was my friend's car stereo that was on the healing bench. He complained that he was unable...

You might also enojy this

How to design a NodeMCU project board with Altium Designer

When it comes to making quick projects I just love the NodeMCU development board. You get I2C, SPI, and a direct Wi-Fi connection without an...