Byte, uint8_t and unsigned char, they are basically the same thing in Arduino. These data types often cause confusions to new programmers. So is there any difference in them?
A byte stores an 8-bit unsigned number, from 0 to 255. For example for the number 0, the binary form is 00000000, there are 8 zeros (8 bits in total). for the number 255, the binary form is 11111111.
A uint8_t data type is basically the same as byte in Arduino. Writers of embedded software often define these types, because systems can sometimes define int to be 8 bits, 16 bits or 32 bits long. The issue doesn’t arise in C# or Java, because the size of all the basic types is defined by the language.
An unsigned char data type that occupies 1 byte of memory. It is the same as the byte datatype. The unsigned char datatype encodes numbers from 0 to 255. For consistency of Arduino programming style, the byte data type is to be preferred.
What are the difference? Which one should I use?
Not really, but It documents your intent for that variable. For example you will be storing small numbers, rather than a character, you will use byte.
Also it looks nicer to use uint8_t if you are also using other similar typedefs such as uint16_t or int32_t. With the number in the type definition, it clearly states how many bits the variable would take up. With or without the “u” at the beginning also gives people an idea it’s signed or unsigned.
The other thing that trips people up is “char”. It can be equivalent to uint8_t or int8_t. Or it might not be 8-bits at all, but that’s fairly rare. On Arduino, char is int8_t but byte is uint8_t.
Anyway, in Arduino, byte, uint8_t and unsigned short can be used interchangeably because they are literally the same type. It’s just an alias. If you are just compiling the sketch on Arduino IDE and upload to the Arduino, use byte should be enough.