| PEARSON CONSULTING | SOFTWARE | SERVICES | PROJECTS | IDEAS | ABOUT |
A1. Hardware support (TWI)
B1. Single Write function
This project provides a minimal I2C Slave that can be used with an Arduino
or other board that can act as an I2C Master.
The four wire interface provides VCC, GND, SCL, and SDA signals. Since VCC is
supplied by the Master board, this design is compatible with 5v and 3.3v
systems provided that the AVR chip supports the voltage level. (2.7v-5.0v) or
(1.8v-5.0v).
The software is designed to run with a Slave using an 8 MHz CPU clock (uncheck
the CKDIV8 fuse when programming). No adjustment is needed to support the
Master provided 100 kHz or 400 kHz SCL clock speeds. However, if a different
CPU clock is used, it must be at least 16 times the SCL clock.
The software is compatible with Atmel Studio 6.2 and is written in 'C'.
Components for a basic A1+B1 Slave device to turn on an LED using an ATmega44/88/168/328, ATmega164/324/644/1284, or any AVR with a TWI hardware section.
![]() |
![]() |
| Schematic A1B1 | Bread Board A1B1 |
Parts List
| 1 | Protoboard |
| 1 | AVR microcontroller (example Atmega88A) |
| 1 | 10uF electrolytic capacitor (optional for power filtering) |
| 1 | LED |
| 1 | 390 ohm 1/4 watt resistor |
| 2 | 6.8k ohm 1/4 watt resistor |
| 4 | Jumper wires for VCC, GND, SCL, and SDA lines. |
| 1 | Arduino UNO |
Copy the code from Slave_A1B1.c
into your main.c for the project and build.
Program the AVR chip and connect up the jumper wires to the Arduino board.
The complete Atmel Studio project can also be downloaded from GitHub.
Slave A1B1
Load the Arduino_A1B1_demo code into
the Arduino IDE and select the board and COM port that you will use. Build and
download into the Arduino board.
If the AVR is connected to the Arduino, the LED should blink on and off.
Process Flow: Like most C programs, the process begins with a call to main()
to start the program.
The first thing that main() does is initialize the LED hardware pin.
It then calls twiSlaveInit( adrs ) to initialize the TWI hardware and pass it
the I2C address of the Slave. The address is a unique 7-bit value and a
different value has to be assigned to each Slave on the I2C bus to prevent
conflicts.
Once the hardware is setup, the interrupts are enabled by calling sei() to set
the Global Interrupt flag.
Finally, the Slave is activated by calling twiSlaveEnable(). After this point,
the Slave will automatically respond to its address and copy data it receives
into its Receive FIFO buffer.
The process now enters an infinite WHILE loop. This simple loop continually
checks for data in the Receive FIFO Buffer by calling twiDataInReceiveBuffer()
and checking for a TRUE return value.
When the IF statement evaluates true, then one byte of data is read from the
buffer and evaluated by the next IF statement.
If the data is 0, then the LED hardware is cleared to 0 to turn the LED off.
Otherwise (for any value other than 0), the LED hardware is set to 1 to turn
the LED ON.
If the LED does not blink, check wiring and that the Slave address being sent by the Master is the same as the one the Slave is set to. Unfourtunatly, an oscilloscope is needed to troubleshoot farther than code inspection and wiring checks.
Now that this project is working, it provdes a baseline of operations.