Sunday, August 28, 2022

IR remote control library for AVR microcontrollers

This library can be used for sending or receiving remote controller codes using a microcontroller. The supported protocols at the moment are NEC and RC-5 since these are the most commonly used.

The IR library is very easy to use and it needs a 16-bit timer for both sending and receiving. Timer 1 is used for this purpose. Before diving into the code, let's see how a remote controller works and take a closer look at their protocols.




How a remote controller works

A remote control is using optical communication to send wireless data to a receiver device. For this purpose, the infrared light was chosen. The data is transmitted on top of a carrier frequency that is usually 38kHz. There are many schemes of encoding the data because there are many manufacturers of consumer products. The most commonly used protocols are NEC, RC-5, RC-6, Sony. 

Sunday, August 7, 2022

UART library for AVR microcontrollers

A highly customizable UART library that can also be called USART since it has the option for synchronous transfer mode. It can work with transmit and receive buffers down to 1 byte in size, making it suitable for devices with small memory. It was mainly made for ATmega328PB.

AVR microcontrollers have support for both UART and USART modes. USART stands for Universal Synchronous Asynchronous Receiver Transmitter. Notice that UART lacks the S that stands for Synchronous.

UART is a type of serial interface, as opposed to a parallel interface. A parallel interface can work at higher speeds but the disadvantage is that it needs multiple input/output lines. Other examples of serial interfaces are SPI and I2C.

UART library for AVR microcontrollers using interrupts


  • Custom Baud rate
  • Asynchronous or synchronous modes
  • Supports serial frames with 5, 6, 7, 8, or 9 data bits
  • Odd, even or no parity
  • Error detection
  • Multi-processor communication mode used to address multiple devices on the same serial bus
  • Double speed asynchronous communication mode

Thursday, August 4, 2022

Tutorial on how to program an AVR ATmega328PB microcontroller using Atmel Studio and a bootloader

In this tutorial you will be learning how to use Microchip Studio (previously known as Atmel Studio) to program an AVR microcontroller over UART using the Optiboot bootloader. The hardware necessary is very inexpensive. All you need is an ISP (In System Programming) module such as USBTinyISP (around 3$) and an USB to Serial adapter that is around the same price and you can even build it yourself if you wish.

Monday, July 25, 2022

Library for MCP4725 DAC for AVR microcontrollers

The MCP4725 is a low-power, high accuracy, single channel, 12-bit buffered voltage output Digital-to-Analog Convertor (DAC) with non-volatile memory (EEPROM). Its on-board precision output amplifier allows it to achieve rail-to-rail analog output swing. The advantage of this DAC is the A0 pin that can be used to control multiple DACs by changing their address (more on that later).

This DAC library also includes an I2C driver that is needed by the AVR to communicate with the MCP4725.

Library for MCP4725 DAC for AVR microcontrollers


  1. Pin Descriptions
  2. Wiring up one or multiple DACs
  3. MCP4725 Characteristics
  4. Code Example
  5. Using the MCP4725 library
  6. Download


1. Pin Descriptions


MCP4725 DAC pin description

MCP4725 pins

Wednesday, July 13, 2022

SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2)

This is part 2 of the tutorial on SD card specifications. In part 1 of the tutorial we made functions necessary for SPI communication and card initialization in SPI mode. At the end of this second part you should be able to read and write an SD card.


5. Reading/Writing Data Blocks

5.1 Block Length

5.2 CMD17 – Reading a Single Block

5.2.1 Read Errors

5.3 CMD24 – Writing a Single Block

6. Conclusion or Confusion

7. Links 

SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2)

5. Reading/Writing Data Blocks

5.1 Block Length

Block length can be set in Standard Capacity SD cards using CMD16 (SET_BLOCKLEN) however for SDHC and SDXC cards, the block length is always set to 512 bytes. Since nowadays most if not all cards are of high capacity type, we will only consider the latter in this tutorial.

SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)

Personally I learn better using practical examples instead of abstract data, and for this reason I have constructed this tutorial as a step by step with practical code examples written in C language, that can be followed by anyone with basic programming skills and knowledge on how to use a microcontroller. Although I am using an ATmega328P in this tutorial, the concepts extend to any microcontroller.

If you need an SD card library, you can find one here: This tutorial will help in understanding the low-level interactions between a microcontroller and a memory card but it will not be enough to use an SD card in a practical way. Apart from an SD card driver, that is the code described in this articles, you will also need a file system driver such as FAT16 or FAT32 in order to read or write files.


1. General Description

1.1 Bus Mode and Clock Speed

1.2 Read/Write Mode Selection

2. SD Card Hardware Interface

2.1 microSD Card Schematic SPI Interface

3. SPI Setup

4. Card Initialization

4.1 Power Up Sequence

4.2 Sending Commands

4.3 Initialization Flow

4.4 CMD0

4.5 Response R1

4.6 CMD8

4.7 Response R7

4.9 CMD58

4.10 Response R3

4.11 ACMD41 & CMD55

5. Reading/Writing Data Blocks

5.1 Block Length

5.2 CMD17 – Reading a Single Block

5.2.1 Read Errors

5.3 CMD24 – Writing a Single Block

6. Links

SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)

1. General Description

The Secure Digital (SD) Card was developed by the SD Association (SDA) as an improvement over MMCs. The SD Card specifications were originally defined by MEI (Matsushita Electric Company), Toshiba Corporation and SanDisk Corporation. Currently, the specifications are controlled by the Secure Digital Association (SDA).

In addition to the mass storage specific flash memory chip, the SD Card includes an on-card intelligent controller which manages interface protocols, security algorithms for copyright protection, data storage and retrieval, as well as Error Correction Code (ECC) algorithms, defect handling and diagnostics, power management and clock control.

Thursday, July 7, 2022

SD Memory Card Library for AVR Microcontrollers - SD and FAT Driver

This project facilitates the reading and writing of SD flash memory cards using FAT16 or FAT32 file systems, over the SPI protocol, and it is designed with embedded systems in mind. It includes an SD driver that uses the SPI interface and a FAT driver that is controlled by the host microcontroller.

If you whish to know about the low level communication between the microcontroller and the SD card, I have a two part series tutorial here

There are two main standards for flash memory cards: MMC (MultiMediaCards) and SD (Secure Digital), SD being the most used today. SD was developed by the SD Association (SDA) as an improvement over MMCs. These cards have basically a flash memory array and a microcontroller inside that controls erasing, reading, writing, error controls, and wear leveling of the flash array. The data is transferred between the memory card and the host controller as data blocks in units of 512 bytes.

The recommended file systems for memory cards are FAT12/16/32 or exFAT. Maximum volume sizes for each one are: 256MB, 4GB, 16TB* for FAT32 and 128PB for exFAT.

*Windows will refuse to format cards over 32GB using FAT32, offering exFAT and NTFS as an option but there are workarounds and third-party software that can do this.


Flash RAM Flash RAM
Read 6.1k 605 6.6k 607
Write 7.8k 607 8.6k 609
Read/Write 7.9k 607 8.7k 609

Some rounded up values of the code footprint depending on the functions used: for reading, writing or both. RAM size is expressed in bytes and it includes 512 bytes for the read/write buffer.


  • Communication protocol: SPI
  • Supported memory cards: SD cards (MMC's are not implemented)
  • Includes SD driver: Yes
  • Supported file systems: FAT16 and FAT32
  • Support for LFN (Long File Names): Yes (up to 255 characters)
  • Formatting utility: No
  • CRC: not yet
  • Multiple files and folders instances: Yes
  • Ability to create folders and directories
  • Delete files: No (only file truncation is implemented)
  • FSInfo (FAT32): not implemented in order to reduce the code size. The only situations when this could be a downside is when querying for free space, creating files/folders or expand them since this is when it is necessary to search for a free cluster. If the card is mostly empty, even if it has a large capacity, the search for a free cluster will be very quick. As the card is filled it will take longer (few seconds).


  1. SD Card Pins
  2. Schematic Interface
  3. Return Values
  4. File Object Structures
  5. File names
  6. The Buffer, the Writing and the Flush
  7. Code Examples
  8. Library Configuration
  9. Functions and their usage
    1. Volume Management
    2. Directory Access
    3. File Access
  10. Download SD Card Library

SD Card Pinout

Both MMC/SD standards have their own proprietary protocols but they also support SPI which can be selected during card initialization. Since microcontrollers have SPI integrated hardware, this is the most used interface for memory cards.