Saturday, October 16, 2021

How the I2C and TWI (Two Wire Interface) protocol works

I2C is a two-wire serial bus communication protocol invented by Phillips in 1982. TWI stands for Two Wire Interface and, for the most part, this bus is identical to I²C. The name TWI was introduced by Atmel and other companies to avoid conflicts with trademark issues related to I²C. Because these two protocols are almost the same I will refer to them interchangeably throughout the course of this article.

This protocol is very used nowadays by all sorts of devices such as DACs, LCDs, sensors, etc. so it's worth learning about it.

As a disclaimer I need to mention that most parts of this article contains fragments from the ATmega328, 324 datasheet and MCP4706 datasheet. I liked how they explained the I2C, TWI protocol  and since not many people read the datasheets I want to share this information with anyone interested in the TWI protocol.

If you're interested in a I2C, TWI library it can be found at this link I2C, TWI library.

Features

  • 7-bit Address Space Allows up to 128 Different Slave Addresses
  • Multi-master Arbitration Support
  • General call addressing

The I2C interface specifies different communication bit rates. These are referred to as Standard, Fast or HighSpeed modes.

  • Standard mode: bit rates up to 100 kbit/s
  • Fast mode: bit rates up to 400 kbit/s
  • High-Speed mode (HS mode): bit rates up to 3.4 Mbit/s

High-Speed mode is currently unsupported by the TWI.

Table of Contents

 

Two-Wire Serial Interface Bus

The TWI protocol is able to interconnect up to 128 different devices using only two bidirectional bus lines: one for clock (SCL) and one for data (SDA). The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol.

TWI Bus Interconnection
TWI Bus Interconnection

The number of devices that can be connected to the bus is only limited by the bus capacitance limit of 400pF and the 7-bit slave address space. The SCL and SDA pins are open-drain configurations. For this reason these pins require a pull-up resistor.

Saturday, May 29, 2021

Bluetooth Stereo Audio Receiver JDY-62 module and the Bluetooth 4.0 CSR dongle transmitter

There are many solutions for streaming audio from your computer to Bluetooth devices. One affordable Bluetooth transmitter/receiver pair is presented here. With these two Bluetooth modules, streaming audio from PC is very easy.

JDY-62 is an affordable Bluetooth stereo audio receiver module with great sound qualities for the price provided the ground connection is properly made (more on this later). To send audio from PC to the JDY-62 module I have used the Bluetooth 4.0 CSR dongle transmitter that works well on Windows 10 and acts like a sound card that you can select in the media player.

Bluetooth Stereo Audio Receiver JDY-62 module and the Bluetooth 4.0 CSR dongle transmitter

JDY-62 module pinout

JDY-62 module pinout

Monday, May 10, 2021

Pinning files on LibreOffice taskbar and other apps in Windows 10 - an alternative to recent files

I was searching a way to show the recent files in LibreOffice by right clicking on the pinned app on the taskbar but it appears it can't be done in Windows 10. But, there is a better way - pinning the most used documents.

Pinning files on LibreOffice taskbar in Windows 10

To pin your favorite files on LibreOffice (or any other apps like folders in Explorer) first pin the application on the Windows taskbar by dragging the icon from the desktop to the taskbar.

Pinning files on LibreOffice taskbar in Windows 10 - 1
Click to enlarge

Then to pin a file, drag the file from Windows explorer to the desired app on the taskbar.

Pinning files on LibreOffice taskbar in Windows 10 - 2


From now on you can just right click on the taskbar app and select which file do you want to open.

Wednesday, April 21, 2021

Building a Digital Clock with RGB Lamp & Spherical Shelf DIY

Here is how to build a simple digital wall clock encased in a spherical shelf that could make a nice gift for someone.

The clock is based on an ATmega328PB microcontroller that is using a 32.768 kHz crystal for time keeping. It also has 6 RGB leds that act as a night lamp or simply for aesthetic purposes.

Digital Clock With RGB Lamp & Spherical Shelf DIY

Thursday, February 25, 2021

Playing music and tones using a piezo buzzer - library for AVR microcontrollers using timer interrupt | ATmega328

This is a library for playing monophonic music using PWM and a piezoelectric buzzer. Monophonic means it can play only one note at a time. Regardless, it can produce some nice music. Optionally a led can be made to blink with the music rhythm.

Apart from playing songs it can also be used for tone generation useful in projects where audio indicators are needed.

All you need is a 16-bit timer that can be one of the following: timer1, timer3 and timer4 (provided that the microcontroller has them) and an 1ms interval interrupt where to place the main function. This is needed because the notes must have a certain duration.

Playing music and tones using a piezo buzzer - library for AVR microcontrollers using timer interrupt | ATmega328P, ATmega88

Playing music on a piezoelectric buzzer using PWM and a microcontroller


Saturday, February 6, 2021

Colorspace conversion between RGB and HSL library code for AVR microcontrollers | ATmega328P

I had a project where I needed to crossfade RGB colors and I thought why not using the HSL color space instead of the RGB color space because with the HSL the code looks neater and the Hue (color), Saturation and Lightness can easily be modified to create all kinds of light effects.

Colorspace conversion between RGB and HSL library code for AVR microcontrollers | ATmega328P

Converting HSL to RGB

HSLtoRGB(hue, saturation, lightness, rgb[])

Thursday, February 4, 2021

How to control RGB leds | RGB fader library for AVR ATmega328P

RGB leds are fun and because they can be used in many projects I have decided to make a library to easily crossfade the colors of one or multiple RGB leds.

To see this library used in a real project, check out this video Digital Clock With RGB Night Lamp & Spherical Shelf.

How to control RGB leds | Crossfading RGB leds | Library for AVR ATmega328P

Crossfading an RGB led in the RGB colorspace

With 8 bits we have 256 values from 0 to 255 that represents the duty cycle - how long a led will be on then off in a period. For example setting the RED led to 255 and GREEN and BLUE to 0 will result in RED color. Or RED 255, GREEN 0 and BLUE 255 will show a purple color. All leds on (255 value) will result in a white lite. So this is how a certain color can be produced but how to cycle through all the possible combinations?

Crossfading an RGB led in the RGB colorspace 1

First the red color is set at 255 and green and blue to 0. Then the red will be decremented and the green will be incremented. When the red will be 0 and green 255 we change the fading up and fading down colors.