Do you know what Arduino is? Do you know what Arduino can do? Today, let’s get to know Arduino!
Arduino is a convenient, flexible, and easy-to-use open-source electronic prototyping platform.
It includes hardware (various models of Arduino boards) and software (Arduino IDE). It was developed by a European team in the winter of 2005. Its members include Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino, David Mellis, and Nicholas Zambetti.
This article shares a case of lighting up an LED using Arduino.
Required Tools
Hardware Devices
-
Arduino Uno Development Board
-
Desktop Computer
-
Arduino to Desktop Connection Cable
Software
-
Atmel Studio
-
Install Atmel Studio, which includes Atmel’s compiler and also installs the virtual comm port driver for printing information to TeraTerm.
-
You can also download the Arduino IDE, which allows for quick programming of Arduino, but I prefer using Atmel Studio.
-
AvrDude
-
This software can download code to Arduino via the serial port.
-
Serial Communication Software, Putty or TeraTerm
-
Code Editing Software, Atom or any text editing software
Documentation
-
Download link for the Atmel328P CPU chip manual.
Starting Your Arduino Journey
Project Introduction
This small project mainly aims to introduce you to the field of embedded development.
-
Light up your own LED light
-
Debug through serial communication
-
Write a simple command line interface to communicate with the board
Getting to Know Arduino Hardware
Our core is the Arduino Uno development board. The following diagram shows the circuit diagram of Arduino. I have marked several major parts, and I will explain them one by one.
-
Voltage Regulator: Its job is to provide stable 3.3V and 5V voltage to the system. In the blue area, there are two voltage regulators, one is LP2985, which inputs 5V and outputs 3.3V; the other is NCP1117, which inputs up to 20V and outputs 5V. Arduino can be powered in two ways, one is via USB power, where it provides 5V voltage from the USBVCC in the lower left corner of the blue area, and then through a regulator to provide 3.3V voltage. The other power supply is through the power socket (there is a round black power socket below the USB socket on the board), which can have a maximum voltage of 20V, then converts to 5V voltage through NCP1117, and then to 3.3V voltage through LP2985. One notable point is that the USBVCC from the blue area is connected to a transistor, which has a comparator on it. The positive input of the comparator is connected to a voltage divider circuit, while the negative input is connected to 3.3V. Its purpose is that if the voltage input from the power socket is less than 5V, it will use the 5V voltage from USB; otherwise, it will use the 5V voltage from the power socket.
-
USB Control Chip: The USB control chip, which comes with the Arduino, has its firmware already inside. Its function is to convert USB interface data into serial communication data (in the red Serial Comm part of the circuit diagram) sent to the CPU, and also to convert data sent from the CPU via serial communication into USB signals sent to the PC.
-
Main CPU: The main CPU is Atmel328P. An 8-bit CPU, because Arduino does not have external serial flash or external SDRAM, according to the chip manual, there is a total of 32KB programming flash on the chip, where the compiled code can be placed. There is 2KB of SRAM, and some register information, stack and heap, global variables, etc., are stored in RAM.
-
Crystal: 16MHz crystal oscillator.
-
LED: The input mark for the LED is SCK, corresponding to pin B5 on the Atmel328P. The LED is connected to an amplifier, which is designed so that the current does not pass through the amplifier, but the voltage controls the LED, allowing pin B5 to be used for other purposes.
-
Serial Comm: Serial communication port, sending data to the PC via USART on the CPU.
Lighting Up the Arduino LED
Creating the Project
-
Select File -> Project -> GCC executable project
-
Enter the project name, and select atmel328p for the chip.
-
Then configure avrdude, select tool -> external tool to start configuring avrdude.
-
Title: avrdude programmer
-
Command:
C:\avrdude\avrdude.exe
Please fill in the path to avrdude. -
Arguments:
-F -V -c arduino -p ATMEGA328P -P COM6 -b 115200 -U flash:w:"$(ProjectDir)Debug\$(ItemFileName).hex":i
Find the current comm port from device manager for COM6.
Lighting Up the LED
-
To light up the LED, you need to configure the PB5 GPIO register. According to the Atmel328P data sheet and the Arduino circuit diagram, outputting a high level makes the LED light up, and outputting a low level makes the LED turn off.
DDRB |= (1 << PB5); // Configure PB5's data direction register
PORTB |= (1 << PB5); // Set PB5 to output high level
PORTB &= ~(1 << PB5); // Set PB5 to output low level
Serial Communication
-
Once you can control the LED switch, it indicates that the compiler and Avrdude code download is also fine. Now, for better debugging of the program, we need to ensure that serial communication works normally, so we can print information to the PC.
-
According to the Arduino circuit diagram, we need to ensure that the serial comm in the red area works normally. The USB controller chip can output data from the USB port to the PC.
-
In the Atmel328P data sheet, section 24, there is a detailed description of USART. For USART, the baud rate must be configured first, and then some transmission modes need to be configured, such as sending 8 bits or 7 bits at a time, whether there is a stop bit, etc.
-
Note that the data sheet provides how to calculate the baud rate into the value required by the register. The calculation formula is related to the board’s crystal frequency. Specifically, see page 227 of the data sheet.
-
During the transmission process, you continuously write the data you want to send into the register, and then the Atmel328P sends it to the USB controller chip through two pins, and then the USB controller chip sends it to the PC.
// Configure USART
UBRR0H = (uint8_t)(BAUDRATE_9600_UBRR >> 8); // Configure baud rate
UBRR0L = (uint8_t)BAUDRATE_9600_UBRR;
UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1 << RXCIE0); // Enable receiving and sending data
UCSR0C = (3<<UCSZ00); // Configure transmission mode, 8 bit data 1 bit stop bit
// Sending data
void USART_Transmit(uint8_t * Data, uint16_t Length)
{
uint16_t i;
for (i = 0; i < Length; i++)
{
/* Wait for empty transmit buffer */
while (!( UCSR0A & (1<<UDRE0)));
/* Put data into buffer, sends the data */
UDR0 = Data[i];
}
}
// Receiving data, using interrupt to receive data
ISR(USART_RX_vect)
{
uint8_t ReceivedData;
ReceivedData = UDR0;
}
Command Line Interface
-
Once it is confirmed that Arduino and the PC can communicate normally, we can start writing the command line interface. As the name suggests, it allows inputting commands from the PC side for Arduino to perform corresponding actions. Most electronic products have their own CLI to communicate with the product, and often when developing new features, a new command is added, and the PC’s driver can send this new command to the embedded device, enabling it to execute new functions.
-
Source code for the command line interface, where you can input commands to turn the Arduino LED on or off.
-
There are some interesting points in the implementation of the command line interface.
-
I used a circular buffer to implement data reception and processing, with a read index and a write index. The purpose of using the buffer is that the speed at which the user inputs commands may differ from the speed at which the computer processes them, so we need a buffer to balance them. For example, if the computer takes a long time to process a command, and the user inputs several other commands in a row after this command, all other commands will be placed in this circular buffer and processed in order.
-
This small project uses the volatile keyword to define a variable,
USART_StartCmdProcess
, to record how many commands are currently in the receive buffer. The reason is that we increment this variable in an interrupt, and when the compiler compiles this piece of code, if it’s not volatile, the compiler would not know when this variable will increment, as the interrupt can occur at any time. Therefore, in the main function, there is an if statement checking if (variable > 0), which the compiler may think will never happen (the compiler assumes this check is always false). So, by adding volatile, it forces the compiler to truly check the variable’s value during compilation, simply put, it will not optimize the if statement in the main function.
-
Input GetLedStatus, Arduino returns LED OFF
-
Input SetLed ON, Arduino lights up the LED
-
Input GetLedStatus, Arduino returns LED ON
-
Input SetLed OFF, Arduino turns off the LED
Leave a Comment
Your email address will not be published. Required fields are marked *