• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    C++
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal I2C master routines for all AVR microcontrollers.

TinyI2C Library

TinyI2C is a set of minimal I2C routines that allow just about any Microchip/Atmel AVR processor to connect to I2C peripherals.

For more information and examples see Tiny I2C Routines for all AVR Microcontrollers.

The main difference between these routines and the standard Arduino Wire library is that these don't need to use buffers, so have much smaller memory requirements and don't impose a limit on transmissions.

Version 2.0.1 increases the number of bytes you can specify in a single transfer.

Compatibility

These I2C routines are designed to provide master I2C functionality for all Microchip/Atmel AVR processors. Over the years different generations of AVR chips have featured three different, incompatible peripherals to handle I2C:

Universal Serial interface (USI) peripheral

The USI provides master I2C support to ATtiny processors with a USI peripheral, namely:

  • ATtiny25/45/85 and ATtiny24/44/84
  • ATtiny261/461/861
  • ATtiny87/167
  • ATtiny2313/4313
  • ATtiny1634

2-Wire Serial Interface (TWI) peripheral

This provides full master I2C support, and is featured in:

  • Most of the original ATmega processors, such as the ATmega328P used in the Arduino Uno, ATmega2560 used in the Arduino Mega 2560, and the ATmega1284P.
  • Two unusual ATtiny processors that provide a TWI peripheral, the ATtiny48 and 88.

Two-Wire Interface (TWI) peripheral

A new version of the TWI peripheral is featured in:

  • The latest ATtiny 0-series, 1-series, and 2-series processors, such as the ATtiny414.
  • The 0-series ATmega chips, such as the ATmega4809.
  • The AVR DA and DB family, such as the AVR128DA48.

These universal Tiny I2C routines provide master I2C support to all three generations of AVR processors.

Differences from Arduino Wire

I've named these routines TinyI2C for two reasons: to distinguish them from the existing Wire libraries, such as the ones included in the Arduino and Spence Konde's cores, and to emphasise that these routines don't follow the Arduino Wire library naming conventions.

In addition, these routines differ from the Arduino Wire library routines in the following ways:

Low memory requirements

These routines don't use buffers, reducing their RAM requirements to a couple of bytes. The standard 0-series ATmega Wire library uses 128-byte send and receive buffers, and the 0-series and 1-series ATtiny Wire libraries use 32-byte or 16-byte buffers, which on the smaller chips is a significant part of the available RAM. As far as I can see there's no need for buffering as the I2C protocol incorporates handshaking, using the ACK/NACK pulses.

Unlimited transmission length

These routines don't impose any limit on the length of transmissions. The standard Wire libraries limit the length of any transmission to the size of the buffer. This isn't a problem with many I2C applications, such as reading the temperature from a sensor, but it is a problem with applications such as driving an I2C OLED display, which requires you to send 1024 bytes to update the whole display.

Flexible read

These routines allow you to specify in advance how many bytes you want to read from an I2C peripheral, or you can leave this open-ended and mark the last byte read. This is an advantage when you don't know in advance how many bytes you are going to want to read.

Polling

For simplicity these routines use polling rather than interrupts, so they won't interfere with other processes using interrupts.

Description

Install TinyI2C into your libraries folder in your Arduino folder and include this at the top of your program:

#include <TinyI2CMaster.h>

Here's a description of the minimal TinyI2C routines:

TinyI2C.init()

Initialises TinyI2C. This should be called in setup().

TinyI2C.start(address, type)

Starts a transaction with the slave device at the specified address, and specifies if the transaction is going to be a read or a write. It returns true if the start was successful or false if there was an error.

Theย type parameter can have the following values:

  • 0: Write to the device.
  • 1 to 2147483647: Read from the device. The number specifies how many bytes you are going to read.
  • -1: Read an unspecified number of bytes from the device.

Ifย type is specified as -1 you must identify the last byte read by callingย TinyI2C.readlast() rather thanย  TinyI2C.read().

TinyI2C.write(data)

Writes a byte of data to a slave device. It returns true if the write was successful or false if there was an error.

TinyI2C.read()

Reads a byte from a slave device and returns it.

TinyI2C.readLast()

Reads a byte from a slave device and tells the slave to stop sending.

You only need to useย TinyI2C.readlast() if you called TinyI2C.start() orย TinyI2C.restart()ย withย type set to -1.

TinyI2C.restart(address, type);

Does a restart. Theย type parameter is the same as forย  TinyI2C.start().

TinyI2C.stop()

Ends the transaction; there's no return value.

Everyย TinyI2C.start() should have a matching TinyI2C.stop().

Using the TinyI2C library

Pullup resistors

You must have pullup resistors on the SCL and SDA lines for I2C to work reliably; recommended values are 4.7kฮฉ or 10kฮฉ. On platforms where this is possible the TinyI2C routines turn on the internal pullups on the SCL and SDA lines as this can't do any harm, but you shouldn't rely on these.

Writing to an I2C device

Writing to an I2C device is straightforward: for example, to write one byte:

TinyI2C.start(Address, 0);
TinyI2C.write(byte);
TinyI2C.stop();

Reading from an I2C device

The TinyI2C routines allow you to identify the last byte read from an I2C device in either of two ways:

You can specify the total number of bytes you are going to read, as the second parameter of TinyI2C.start(). With this approachย TinyI2C.read() will automatically terminate the last call with a NAK:

TinyI2C.start(Address, 2);
int mins = TinyI2C.read();
int hrs = TinyI2C.read();
TinyI2C.stop();

Alternatively you can just specify the second parameter ofย TinyI2C.start() asย -1, and explicitly identify the lastย TinyI2C.read command by callingย TinyI2C.readlast():

TinyI2C.start(Address, -1);
int mins = TinyI2C.read();
int hrs = TinyI2C.readLast();
TinyI2C.stop();

Writing and reading

Many I2C devices require you to write one or more bytes before reading, to specify the register you want to read from; the read should be introduced with aย TinyI2C.restart() call; for example:

TinyI2C.start(Address, 0);
TinyI2C.write(1);
TinyI2C.restart(Address, 2);
int mins = TinyI2C.read();
int hrs = TinyI2C.read();
TinyI2C.stop();

More Repositories

1

ulisp

A version of the Lisp programming language for ATmega-based Arduino boards.
C++
351
star
2

lisp-badge

A handheld computer programmed in Lisp with a self-contained keyboard and display.
C++
109
star
3

ulisp-esp

A version of the Lisp programming language for ESP8266 and ESP32-based boards.
C++
102
star
4

ulisp-arm

A version of the Lisp programming language for ARM-based boards.
C++
86
star
5

tiny-lisp-computer

A self-contained computer with its own display and keyboard, based on an ATmega328 or ATmega1284, that you can program in Lisp.
Eagle
68
star
6

attiny10core

For programming the ATtiny10/9/5/4.
C
60
star
7

ulisp-zero

A pared-down version of uLisp for hackers.
Arduino
48
star
8

lisp-riscv-assembler

A RISC-V assembler written in Lisp.
Common Lisp
37
star
9

tiny-bme280

An interface to allow you to use the Bosch BME280 sensor from an ATtiny processor.
C++
34
star
10

lisp-arm-assembler

An ARM Thumb assembler written in Lisp.
Common Lisp
34
star
11

minimal-rp2040-board

A minimal breakout board based on the Raspberry Pi RP2040 processor.
26
star
12

tiny-function-generator

A signal generator based on an ATtiny85 that generates a range of different waveforms
C++
26
star
13

ulisp-stm32

A version of the Lisp programming language for STM32-based boards
C++
23
star
14

tiny-tft-graphics-library

A small graphics library for ATtiny microcontrollers that supports a variety of different colour TFT displays.
C++
23
star
15

frequency-probe

A handheld tool that gives a visual indication of the frequency or voltage at the probe.
C++
22
star
16

i2c-detective

Identify I2C devices from a database of the most popular I2C sensors and other devices
C++
22
star
17

tiny-mega-i2c

Minimal I2C master routines for the new AVR microcontrollers.
C++
21
star
18

ulisp-riscv

A version of the Lisp programming language for RISC-V based boards.
C++
21
star
19

universal-tft-display-backpack

A microcontroller board that can accommodate a range of different colour TFT displays.
18
star
20

ulisp-builder

Builds a version of uLisp for a particular platform from a common repository of source files
Common Lisp
17
star
21

two-digit-thermometer

A two-digit LED thermometer using a DS12B20 1-Wire temperature sensor, and an ATtiny84.
C++
16
star
22

visible-lisp-computer

A Lisp interpreter that displays the contents of the Lisp workspace on an OLED display
C++
16
star
23

compact-tft-graphics-library

A graphics library using SPI that supports a variety of different colour TFT displays.
C++
14
star
24

continuity-tester

A simple continuity tester, based on an ATtiny85 and a piezo buzzer.
Arduino
14
star
25

atmegabreadboard

For programming a stand-alone ATmega328 or ATmega168.
14
star
26

lisp-badge-le

A self-contained low-power computer with its own display and keyboard that you can program in Lisp.
C++
13
star
27

power-deliverer

Displays a list of the voltages and currents available from a USB-C power adapter and allows you to select one.
C++
13
star
28

tiny-time-watch

ATtiny85-based watch showing the time on 12 LEDs.
Arduino
11
star
29

minimal-gif-decoder

A GIF image decoder designed to allow GIF images to be read and displayed by a small microcontroller.
C++
10
star
30

updi-programmer-stick

A USB-stick sized UPDI programmer
10
star
31

ulisp-msp430

A version of the Lisp programming language for MSP430-based LaunchPad boards
C++
10
star
32

logic-lab

Provides a selection of 12 logic gates that you can interconnect with patch cables to make a variety of different logic circuits.
10
star
33

tiny-time-2-watch

A watch based on an ATtiny85 and a DS2417 RTC showing the time on 12 LEDs.
C++
8
star
34

gamebone

A simple handheld electronic game based on an ATtiny85.
C++
7
star
35

serpente-core

An Arduino core for the Serpente ARM boards
C
7
star
36

ulisp-tdeck

A version of uLisp to convert the LilyGO T-Deck into a self-contained handheld Lisp computer.
C++
7
star
37

simple-dataflash

A simple DataFlash library for sequential reading and writing.
C++
6
star
38

op-amp-lab

A self-contained tool for experimenting with the configurable op amps in the AVR DB-series.
C++
6
star
39

tiny-face-watch

A watch based on an ATtiny85 and a DS2417 RTC, showing the time analogue-style on a 64x48 OLED display.
Eagle
6
star
40

ir-remote-wand

A simple universal remote control based on an ATtiny85.
C++
5
star
41

ulisp-serpente

A version of the Lisp programming language for Serpente ARM boards
C++
5
star
42

illuminated-button-matrix

A 4 x 4 matrix of illuminated pushbuttons with a two-wire I2C interface.
C++
5
star
43

lcd-clock

A low-power LCD clock based on an AVR128DA48.
C++
5
star
44

ulisp-esp32

A version of the Lisp programming language for ESP32-based boards
C++
5
star
45

query-language

A query language and ATtiny database written in uLisp.
Common Lisp
4
star
46

i2c-sd-card-module

An SD-card module that allows you to write to and read from files on an SD card using I2C.
C++
4
star
47

alphanumeric-display

An eight-character alphanumeric LED display with an I2C interface.
C++
4
star
48

tiny-machine-code-monitor

A machine-code monitor that you program from a hexadecimal keypad using a simplified instruction set called MINIL.
C++
3
star
49

mega-tiny-time-watch

A watch based on an ATtiny414 showing the time on 12 LEDs.
C++
3
star
50

attiny10-thermometer

A thermometer based on an ATtiny10 that displays the temperature on an LED.
C++
3
star
51

lisp-star

A star-shaped pendant that you can program in Lisp
2
star
52

ulisp-wio-terminal

C++
2
star
53

secret-maze

A secret maze game for the ATtiny85
C++
2
star
54

ulisp-bignums

A small arbitrary precision extension for uLisp, for calculating with large integers.
C++
2
star
55

five-leds-puzzle

Press the buttons to light up all the LEDs
C++
2
star
56

attiny10-pov-pendant

A miniature pendant based on the ATtiny10.
C++
2
star
57

tiny-colour-watch

A watch based on an ATtiny85 using 12 APA102 RGB LEDs, arranged like a clock face, to show the time analogue-style.
Arduino
2
star
58

morse-pendant

A pendant that transmits a secret message in morse code by flashing an LED.
C++
2
star
59

twinkling-pendant

A star-shaped pendant with six coloured LEDs that twinkle in a random pattern.
C++
2
star
60

ulisp-library

C++
1
star
61

ulisp-i2c-detective

A Lisp program for identifying I2C devices on the bus
Common Lisp
1
star
62

ulisp-neopixels

An extension for uLisp to drive NeoPixel LED displays from a variety of different platforms.
C++
1
star
63

number-maze-game

A handheld game that displays logic mazes you have to solve by pressing the buttons.
C++
1
star
64

i2c-gps-module

C++
1
star
65

signal-generator

A programmable signal generator capable of generating a square wave from 1kHz to 68MHz.
C++
1
star
66

ulisp-avr-interrupts

Test version of uLisp with interrupt handling
C++
1
star
67

16-leds-puzzle

A puzzle based on an ATtiny204 in which you have to press the right buttons to light up all the LEDs.
C++
1
star