Understanding the Arduino Hardware

I will attempt to explain how the hardware works in this piece as far as I understand it at the moment.

Arduino2

ArduinoSch

So I decided to understand how the arduino works behind the scenes. I wanted to know what components it was made up of and how the components come together to form the whole system. Like every complex system , it is modular. There is the power system, that chooses between the power jack and the USB power and produces 5v and 3v3 for the rest of the system. There is a USB to serial controller that contains the bootloader which is used to load programs into the ATMEGA328P.

The Power System

Power1

If there is a power suply connected to the power jack. The power supply should be between 9v and 15v. The two chips are voltage regulators that produce 5v. I still dont understand why there are two voltage regulator chips.

The Vin is directly connected to the power supply.

Power2

Vin is connected to the LM385D chip which contains two op amps. One of the opamps is used as a comparator and the other is not used at all. 3V3 is compared to Vin/2 if Vin/2>3V3 then the output is 5v and that is used to turn off the supply from the USB. The 5v generated is then connected to the LP2985 chip which is a voltage regulator that takes in 5v and outputs 3v3 .

If instead Vin/2<3.3 , the mosfet is turned on and the USB power is used instead. USB provides the 5v to the LP2985 chip.

And that’s the power system.

USB to Serial Controller

The USB controller is implemented with the ATMEGA8U2-MU. It contains the bootloader code which is used to load the code unto the ATMEGA328P.

USBController

This includes support circuitry like the silicon crystals and the pull-up resistors. There is also the ICSP interface that can be used to program the chip. The ICSP connects to the microcontroller SPI interface.

The ATMEGA328P

This is the main controller. The code you write for the arduino is executed by this controller. And it is directly connected to the I/O pins. The controller is programmed via the TX,RX pins connected to the USB to serial controller

ATMEGA328P

 

And that’s it. This is relatively high level explanation. I will stop here for now. Next up, would be an explanation of the software.

 

Understanding the Adafruit DHT Library

I was working on a getting an IoT demo running on the raspberry pi and so I downloaded the Adafruit DHT library. I want to be able to create libraries like this in future so I took a peek inside.

The Adafruit DHT library is written in Python with C extensions. The C code is used to communicate directly to the DHT sensor and I would assume the reason for this is that C code is faster than Python code in general (always? ). The Python code is written with several layers and it works with the Raspberry Pi 1, the Raspberry Pi 2 and the Beaglebone black.

DescriptionOfCodeStructure

It starts with the __init__.py file. This file contains just one line of code

from common import DHT11, DHT22, AM2302, read, read_retry

The point of this is to make it possible to refer to Adafruit_DHT.read(). The common.py file detects the platform on which the program is run ( Pi 1, Pi2, or Beaglebone) and then call the appropriate read function for each one. Platform specific code is under the files named Raspberry_Pi.py, Raspberry_Pi_2.py and the Beaglebone_black.py. These files are now used to make calls to C extensions installed by setup.py. setup.py installs C extensions based on _Raspberry_Pi.c and other files on the same level (check the diagram). _Raspberry_Pi.c contain details of the interface between the C and Python code. pi_dht_read.c takes care of communication with the DHT sensor. The pi_mmio.c is a fast memory-map for memory-mapped io (I am yet to understand why this is required or what it really means).

What I liked was the beauty and the structure of the program, it was very well organized and it reminds me of one of the lessons I learnt from SICP about how programs are built in layers of abstraction. As it is, it wont be hard to extend this library for another platform just because of the way it is written. Bravo!

I need to imbibe and do this in my own programs

You can find the code on github here