Conversion
Lesson 60: Using byte() Conversion
Learn how byte() converts values into 8-bit range, when to use casting, and how to avoid wrap-around mistakes.
Progress indicator
Lesson 60 of 60
Learning Objectives
- Understand what byte() conversion does in Arduino.
- Know how values map into 8-bit unsigned range (0-255).
- Differentiate byte() casting from byte variable declaration.
- Predict wrap-around behavior for large and negative values.
- Use byte() safely in practical sensor and PWM-style conversions.
Concept Explanation
What is byte() Conversion
byte() converts a value into Arduino byte type.
Arduino byte is an unsigned 8-bit integer, so it stores values from 0 to 255.
byte() Syntax
byte result = byte(value);
byte level = byte(analogValue / 16);How byte() Works
- Evaluate source expression first (int math happens first).
- Take resulting numeric value.
- Keep only low 8 bits.
- Store final value as byte.
Type Casting Concept
Type casting means forcing a value into a different type explicitly.
int x = 300;
byte b = byte(x); // b becomes 44 (300 wraps in 0..255 range)byte() vs byte Variable
| Pattern | Meaning | Example |
|---|---|---|
byte value; | Declaration | byte pwm; |
byte(expr) | Conversion/Cast | byte(raw / 4) |
When to Use byte()
- Hardware APIs or data frames expecting 8-bit values.
- Compact storage for small levels (e.g., PWM 0-255).
- Bit-mask operations and register-style programming.
Example Code
This sketch converts positive, negative, and scaled values to byte and prints them.
const int LED_PIN = 2;
int sensorValue = 300;
int negativeValue = -20;
int pwmRaw = 1023;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
byte convertedSensor = byte(sensorValue);
byte convertedNegative = byte(negativeValue);
byte scaled = byte(sensorValue / 2);
byte pwmLevel = byte(pwmRaw / 4);
if (convertedSensor > 127) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
Serial.print("sensorValue=");
Serial.print(sensorValue);
Serial.print(", byte(sensorValue)=");
Serial.print(convertedSensor);
Serial.print(", byte(negativeValue)=");
Serial.print(convertedNegative);
Serial.print(", scaled=");
Serial.print(scaled);
Serial.print(", pwmLevel=");
Serial.println(pwmLevel);
delay(800);
}Example Code Explanation
byte(sensorValue)converts300to wrapped byte value.byte(negativeValue)converts negative input into 0-255 mapping.scaledshows conversion after arithmetic expression.pwmLeveldemonstrates common scaling before byte casting.- LED threshold check uses converted value to make output decision.
- Serial output shows original and converted values side by side.
What Happens Inside
- Expression is evaluated in larger integer type.
- Conversion keeps only low 8 bits.
- Final byte result is always in 0..255.
- Result is then used by LED logic and Serial printing.
| Input | byte(input) | Note |
|---|---|---|
| 300 | 44 | 300 wraps to 44 |
| -20 | 236 | Negative maps into unsigned byte range |
| 255 | 255 | Already in range, unchanged |
Common Mistakes with byte()
- Assuming conversion clamps values instead of wrap-around.
- Ignoring behavior of negative values after conversion.
- Converting too early and losing useful precision.
- Using byte where a wider type is required for math safety.
Best Practices for byte()
- Perform main calculations in int/long, cast at the end.
- Document expected range before conversion.
- Use Serial logs to verify conversion behavior during development.
- Use explicit scaling (like `/ 4`) before casting sensor values.
Try it now
Open the simulator workspace and inspect byte() conversion outputs step-by-step.