Arduino is a microcontroller platform that has to deal with a number of different bits to send and receive data to different peripherals. So, Arduino programming includes a series of byte functions to manipulate this data. One such function is lowByte(). This function allows us to extract the lower byte from the data. This article contains a complete guide on lowByte() function and explores its syntax, parameter and return value.
What is Arduino lowByte() Function
The lowByte() function allows the user to extract the lower 8 bits of a 16-bit integer. It extracts the rightmost low order byte of the variable. This function is useful when data is split into two bytes, such as analog-to-digital converter (ADC) readings, pulse-width modulation (PWM) values, or any other data type that uses two bytes.
Syntax
The lowByte() function has a very simple syntax:
Parameter
It takes one parameter “x” that is the 16-bit integer that you want to extract the lower 8 bits from.
Return
This function returns the byte (8 bits) of data representing the lower 8 bits of the input x integer.
Example of using lowByte()
This code is an example program for the Arduino microcontroller that demonstrates the usage of the lowByte() function.
Serial.begin(9600);
// Declare and initialize an integer variable
int variable = 1234;
// Print the value of the variable in hexadecimal format
Serial.print("VARIABLE: 0x");
Serial.println(variable, HEX);
// Use the lowByte() function to extract the lower 8 bits of the variable
byte low_byte = lowByte(variable);
// Print the value of the low byte in hexadecimal format
Serial.print("LOW BYTE: 0x");
Serial.println(low_byte, HEX);
}
void loop() {
// The loop function is empty
}
The setup() function is called once at the beginning of the program and is used to perform any initialization tasks. This function starts serial communication.
Next, an integer variable named variable is declared and initialized with the value 1234.
The value of the variable is then printed to the serial monitor in hexadecimal format using the Serial.println() function with the HEX parameter.
The lowByte() function is then used to extract the lower 8 bits of the variable and store the result in a new byte variable called low_byte.
The value of low_byte is then printed to the serial monitor in hexadecimal format using the Serial.println() function with the HEX parameter.
Conclusion
The lowByte() function can make working with bytes of data in Arduino projects much easier. By allowing you to easily extract the lower 8 bits of a 16-bit integer, this function can save you a lot of time and effort. This article explains the lowByte() function, its syntax, parameter and return value.