Notes For Micro Updated
Notes For Micro Updated
The Arduino Uno is a popular microcontroller board that is widely used for various
embedded systems and DIY projects. It is based on the ATmega328P microcontroller from
Atmel (now part of Microchip Technology). I'll provide an overview of the instruction set from
both a hardware and programming perspective:
Hardware
(ATmega328P)
6. I/O Instructions:
- `IN` and `OUT`: Read from and write to I/O registers.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
Programming
(Arduino IDE/C++)
When you write code for an Arduino Uno using the Arduino IDE (which uses a simplified
version of C++), you don't directly interact with the assembly-level instruction set. Instead, you
write code in a higher-level language that is compiled and uploaded to the microcontroller.
1. Arduino Libraries: The Arduino platform provides various libraries that simplify
common tasks, such as handling digital and analog I/O, working with sensors, and
communicating over various protocols (e.g., SPI, I2C).
2. Setup and Loop Functions: An Arduino sketch includes two main functions:
`setup()` and `loop()`. Code in `setup()` is executed once when the microcontroller
starts, and code in `loop()` is executed repeatedly.
3. Digital and Analog I/O: You can use functions like `digitalWrite()`, `digitalRead()`,
`analogWrite()`, and `analogRead()` to interact with pins on the Arduino.
5. External Libraries: You can include external libraries in your Arduino projects to
extend the functionality. These libraries encapsulate complex operations and provide
higher-level abstractions.
I/O interfacing with the Arduino Uno microcontroller involves connecting and controlling
various input and output devices to and from the board. Here, I'll discuss the hardware and
programming aspects of I/O interfacing, as well as the sub-topics you mentioned.
• Hardware:
- Input devices may include sensors like temperature sensors (e.g.,
DHT22), light sensors (e.g., LDR), or switches.
- Output devices can be LEDs, motors, displays, or even communication
modules like Wi-Fi or Bluetooth.
• Programming:
- To interface with input devices, you typically use functions like
`digitalRead()` and `analogRead()`. For example, `digitalRead()` is
used to read the state of digital pins connected to switches or digital
sensors.
- To control output devices, you use functions like `digitalWrite()` and
`analogWrite()`. For example, `digitalWrite()` is used to turn on or off
LEDs.
2. Interface Devices:
• Hardware:
- Interface devices might include level shifters, motor drivers, display
modules (e.g., LCD or OLED screens), and communication modules
(e.g., Wi-Fi, Ethernet shields).
• Programming:
- Depending on the interface device, you might need to include specific
libraries or write custom code to communicate with them. Libraries often
simplify the interaction with devices like displays or sensors.
3. Time-Based I/O:
• Hardware:
- For time-based I/O, you can use timers and counters within the
microcontroller to generate precise timing intervals for tasks like pulse
generation, measuring time, or generating PWM signals.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
• Programming:
- The Arduino platform provides functions to configure and utilize timers,
such as `millis()`, `micros()`, and `delay()`. These functions help you
create time-based routines and manage time-sensitive tasks.
4. Handshaking:
• Hardware:
- Handshaking is a method to synchronize communication between
devices. It often involves control lines like "ready" and "acknowledge."
• Programming:
- Handshaking can be implemented in software using control flags or
variables that signal when data is ready to be read or when a device is
ready to receive data. This is especially useful in applications involving
serial communication protocols like UART.
Here's an example of interfacing a simple LED as both an input and an output device:
• Hardware Setup:
- Connect an LED and a current-limiting resistor to a digital output pin
(e.g., pin 13) on the Arduino.
- Connect a push-button switch to another digital input pin (e.g., pin 2).
• Programming (Example):
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
In this example, the button is an input device, and the LED is an output device. The program
checks the button's state and turns the LED on when the button is pressed. I/O interfacing on the
Arduino Uno is flexible and versatile, allowing you to interact with a wide range of devices and sensors
to create various projects. The key is to understand the hardware connections and use the appropriate
programming techniques and libraries to control and read from these devices.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
Dot matrix displays are something that all Arduino enthusiasts come across at some point. These displays
are so popular that almost all modern outdoor LED displays use them to display characters, symbols, and images.
When it comes to controlling dot-matrix displays, there is hardly a better option than the MAX7219. It can easily
control a single dot matrix and, for larger projects, it can be chained together to control two or more dot matrices.
There are several MAX7219 breakout boards available, two of which are more popular: the generic
module and the FC-16 module.
A typical MAX7219 module includes an 8×8 dot matrix display and a MAX7219 LED display driver. Let’s
get familiar with them.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
An 8×8 dot matrix display typically has 16 pins, 8 for each row and 8 for each column. All rows and
columns are wired together in order to reduce the number of pins. If this were not the case, an 8×8 dot matrix
display would require 65 pins, one for each LED and one for a common anode or common cathode connector. By
connecting rows and columns, only 16 pins are needed to control the entire matrix. This technique of controlling a
large number of LEDs with fewer pins is referred to as Multiplexing.
In this technique, each column is activated for a very short time, and at the same time, the LEDs on that
column are lit by addressing the corresponding row. As a result, no more than eight LEDs are lit at the same time.
The columns are switched so fast (hundreds or thousands of times per second) that the human eye perceives the
display as fully lit.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
MAX7219 Chip
The only problem with multiplexing is that you have to refresh the display all the time to keep the image
stable.
Then there’s the MAX7219 Chip, which handles all of the control and refresh work for you. All you have
to do is send it serial commands through the 4-pin SPI interface, and it will take care of the rest.
It can fully control 64 individual LEDs while keeping their brightness constant. Once the microcontroller
has updated the display, the MAX7219 handles the work of refreshing the display at 800 Hz. This frees up the
microcontroller to do other important things.
The MAX7219 has a power saving mode in which the display can be turned off to save power. It also
turns off the LEDs during startup, preventing wacky displays for the first few seconds of operation.
The MAX7219 communicates via the SPI interface, so it only needs 3 data pins to connect to a
microcontroller. In addition, we can daisy-chain multiple modules together for a larger display using the same 3
wires.
The MAX7219 breakout board includes a resistor (RSet) for adjusting the brightness at the hardware
level.
This resistor controls the maximum current supplied to the LEDs and, hence, the overall brightness of
the display.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
The table below shows the resistor values you should use based on the voltage and forward current of your LED
matrix. A 2V 20 mA LED, for instance, would require a 28kΩ resistor.
Regardless of which variant you select, the module will have two connectors.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
Input Connector
The breakout pins on one end of the module are used to communicate with the microcontroller.
• VCC is connected to 5V. Because the display draws a lot of current (up to 1A at maximum
brightness), it’s best to use an external power supply instead of the Arduino’s 5V supply. If you
want to use the Arduino’s 5V supply, keep the brightness below 25% to avoid overheating the
voltage regulator.
• DIN is the Data pin. Connect it to any digital pin of the microcontroller.
• CS/LOAD is Chip Select (sometimes labeled as LOAD). Connect it to any digital pin of the
microcontroller.
• CLK stands for Clock pin. Connect it to any digital pin of the microcontroller.
Output Connector
• DOUT is Data Out and connects to the DIN pin of the next module.
Now that we know everything about the module, we can start hooking it up to our Arduino!
Let’s start with the module’s power supply connections. Because the display consumes a lot of current,
we’ll use an external power supply instead of the Arduino board’s 5V supply. If you are only using a single
MAX7219 module, you can power it directly from the Arduino, but you should avoid doing so if possible.
Let’s wire up the SPI pins. Note that each Arduino board has a unique set of SPI pins that must be
connected accordingly. For Arduino boards such as the UNO/Nano V3.0, these pins are digital 13 (SCK), 12
(MISO), 11 (MOSI), and 10 (SS).
If you’re using a different Arduino board, check the official documentation for SPI pin locations before
proceeding.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
Here is the wiring for the Generic MAX7219 Module:
5V GND
Library Installation
Controlling the MAX7219 module is a lot of work. Fortunately, the MD Parola library was written to hide
the complexities of the MAX7219, allowing us to control the display with simple commands.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
To install the library, navigate to Sketch > Include Library > Manage Libraries… Wait for the Library
Manager to download the library index and update the list of installed libraries.
Filter your search by entering ‘max72xx’. Look for MD_MAX72XX by MajicDesigns. Click on that entry
and then choose Install.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
The MD_MAX72XX library is a hardware-specific library that handles lower-level functions. It must be
used in conjunction with the MD_Parola Library to create a variety of text animations such as scrolling and sprite
text effects. Install this library as well.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
Arduino Example Code 1 – Printing Text
Our first experiment involves displaying a simple text without any animation.
But, before you upload the sketch, you must modify the following two variables.
The first variable, HARDWARE_TYPE, informs the Arduino of the module variant.
• Set the HARDWARE_TYPE to GENERIC_HW, if you’re using a module with a green PCB and
a through hole MAX7219 IC like the one shown below.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
• Set the HARDWARE_TYPE to FC16_HW, if you’re using a module with a blue PCB and an
SMD MAX7219 IC like the one shown below.
The second variable, MAX_DEVICES, specifies the number of MAX7219 ICs being used. A single MAX7219 IC
counts as one device, so if you want to control an 8×32 display, set MAX_DEVICES to 4 because an 8×32
display has four MAX7219 ICs.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
The first step is to include all the necessary Arduino libraries. As previously stated, the MD_MAX72XX library
implements the hardware-specific functions of the LED matrix, whereas the MD_Parola library implements the text
effect. You must also include the SPI library, which is used to communicate with the display via SPI.
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
Next, we must specify which hardware is being used. Because we are using an FC-16 module for our experiments,
the HARDWARE_TYPE is set to FC16_HW. We’re using 4 MAX7219 ICs, so MAX_DEVICES is set to 4. Finally,
the pin to which the display’s CS pin is connected is defined.
The function MD_Parola() is then used to create a new instance of the MD_Parola class. The first parameter is the
hardware type, the second is the CS pin, and the third is the number of MAX7219 ICs being used.
In the setup section of the code, we first use the function begin() to initialize the object. The brightness of the display
can be adjusted using the function setIntensity(), which accepts values ranging from 0 (lowest brightness) to 15
(maximum brightness). The display is cleared using the displayClear() function.
void setup() {
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.displayClear();
}
In the loop section of the code, we first set the alignment of the text to be printed with the function
setTextAlignment(), to which the values PA_LEFT, PA_CENTER, and PA_RIGHT can be passed to align the text
to the left, center, or right, respectively.
The string ‘Left’ is then printed using myDisplay.print("Left"). Please keep in mind that the text string should be
enclosed in quotation marks " ". When printing numbers, no quotation marks are required; for example, to display
1234, write myDisplay.print(1234). You can also use the setInvert() function to invert the display.
void loop() {
myDisplay.setTextAlignment(PA_LEFT);
myDisplay.print("Left");
delay(2000);
myDisplay.setTextAlignment(PA_CENTER);
myDisplay.print("Center");
delay(2000);
myDisplay.setTextAlignment(PA_RIGHT);
myDisplay.print("Right");
delay(2000);
myDisplay.setTextAlignment(PA_CENTER);
myDisplay.setInvert(true);
myDisplay.print("Invert");
delay(2000);
myDisplay.setInvert(false);
myDisplay.print(1234);
delay(2000);
}
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
When you want to print a message on a dot matrix display, you will often find that the display is too small to fit the
whole message. The solution is the scroll text effect.
You’ll notice that everything from the beginning of the code to the end of the setup section is the same as the
previous example. The only difference is that the displayScroll() function is called at the end of the setup section.
As you can see, this function takes four arguments: displayScroll(pText, align, textEffect, speed)
Only two functions are used to scroll text in the loop section. First, in an if statement, we use the function
displayAnimate(). This function scrolls the text and returns true when it is finished. When the scrolling is finished,
we use the function displayReset() to reset the display, resulting in continuous scrolling.
void loop() {
if (myDisplay.displayAnimate()) {
myDisplay.displayReset();
}
}
Summary:
This code is an Arduino sketch that uses the MD_Parola library to control a LED matrix display with a
MAX72XX driver using hardware SPI communication. The code displays different text messages and
numeric values on the LED matrix display with various alignments and visual effects.
1. Include Libraries:
2. Hardware Configuration:
You can choose the hardware type by uncommenting one of the two #define statements:
• #define HARDWARE_TYPE MD_MAX72XX::FC16_HW: This specifies the hardware
type as FC16 (a common LED matrix module).
• #define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW: Alternatively, you can use
a generic MAX72XX-based display.
3. Constants:
• MAX_DEVICES is set to 4, indicating that there are four MAX72XX devices in the daisy-
chain.
• CS_PIN is set to 3, indicating the Arduino digital pin used for chip select in the SPI
communication.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
5. setup() Function:
6. loop() Function:
In the loop() function, a series of text and numeric values are displayed on the LED matrix in
different alignments and with or without inversion effects.
• myDisplay.setTextAlignment(PA_LEFT): Sets the text alignment to the left.
• myDisplay.setTextAlignment(PA_CENTER): Sets the text alignment to the center.
• myDisplay.setTextAlignment(PA_RIGHT): Sets the text alignment to the right.
• myDisplay.setInvert(true): Inverts the display (text appears as white on a black
background).
• myDisplay.setInvert(false): Disables the inversion.
• myDisplay.print(): Displays the specified text or numeric value on the LED matrix.
• delay(2000): Pauses for 2 seconds between each display.
• myDisplay.displayAnimate(): Checks if the animation is completed.
• myDisplay.displayReset(): Resets the display.
The code essentially demonstrates how to use the MD_Parola library to control a MAX72XX-based LED
matrix display, showing different text alignments and visual effects. The LED matrix will continuously
cycle through the specified messages and effects in the loop() function.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
Printing Text
// Create a new instance of the MD_Parola class with hardware SPI connection
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
void setup() {
// Intialize the object
myDisplay.begin();
void loop() {
myDisplay.setTextAlignment(PA_LEFT);
myDisplay.print("Left");
delay(2000);
myDisplay.setTextAlignment(PA_CENTER);
myDisplay.print("Center");
delay(2000);
myDisplay.setTextAlignment(PA_RIGHT);
myDisplay.print("Right");
delay(2000);
myDisplay.setTextAlignment(PA_CENTER);
myDisplay.setInvert(true);
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Spirituality
myDisplay.print("Invert");
delay(2000);
myDisplay.setInvert(false);
myDisplay.print(1234);
delay(2000);
}
Scrolling Text
void setup() {
// Intialize the object
myDisplay.begin();
void loop() {
if (myDisplay.displayAnimate()) {
myDisplay.displayReset();
}
}