🍞
Breadstick
  • Breadstick Innovations Website
  • Breadsticks
    • đŸĨ–Raspberry Breadstick
      • Code Examples
        • Demo Code
        • POV Wand
        • Pride Flags POV Wand
        • 6-Axis IMU
        • RGB Blink
        • AsyncIO RGB Blink
        • Digital Input
        • Digital Output
        • PWM
        • ADC
        • Servo Motor
    • đŸĨ–Raspberry Breadstick Lite
    • 🍞Support Boards
      • I2C Devices
        • 🚌I2C Bus Rail Adapter
        • đŸŒĻī¸Weather Crouton
        • 😎Brightness Crouton
        • 📏Distance Crouton
        • đŸĢ¨Motion Crouton
      • I2S Devices
        • đŸ“ĸBoombox
      • SPI Devices
      • đŸ•šī¸Buttons/Switches
    • Learning Resources
    • Troubleshooting
  • Nougat
    • Nougat C3
    • Nougat Quad
    • Installing WLED
  • Pico Slices
    • 🔴Slice 1 - LED Mixer
      • Assembly Guide
      • CircuitPython Code
        • 1 - Blink
        • 2 - Analog Read to Plotter
        • 3 - PWM Fade
        • 4 - Pot Controlled PWM
        • 5 - Gamma Correction
    • âąī¸Slice 2 - Stopwatch
      • Assembly Guide
      • Coding Lessons
        • 1 - 7-Segment Display Intro
        • 2 - Cycling Through All Segments
    • âŦœSlice 3 - 8x8 Dot Matrix
      • Assembly Guide
      • MicroPython Code
        • 1 - Moveable Pixel
        • 2 - Snake
    • Circuit Python Setup
    • Reset Bricked Pico
  • Christmas
    • Christmas Tree DIY kit
  • Protoboards
    • â¤ī¸Proto-Heart
    • đŸĨĒProto-Toast
  • SHOP
Powered by GitBook
On this page
  • Video
  • Gamma Correction
  • Code

Was this helpful?

  1. Pico Slices
  2. Slice 1 - LED Mixer
  3. CircuitPython Code

5 - Gamma Correction

Our eyes don't respond linearly, so we shouldn't increase our LED's brightness linearly. A little math goes a long way.

Previous4 - Pot Controlled PWMNextSlice 2 - Stopwatch

Last updated 2 years ago

Was this helpful?

Video

Gamma Correction

Code

"""
Pico Slice 1 - RGB Mixer
Tutorial 5 - Gamma Correction
"""

import time
import board
from pwmio import PWMOut
from analogio import AnalogIn

# A funcion to handle gamma correction
def Gamma_Correct (linear_input):
    gamma = 3
    maximum = 65535
    output = int(((linear_input/maximum)**gamma)*maximum)
    return output

# Represent the Pot in code as an AnalogIn object.
r_pot = AnalogIn(board.A2)

# Represent the Red LED as a PWMOut object.
r_led = PWMOut(board.GP2, duty_cycle=0, frequency=5000, variable_frequency=False)


while True:                                     # Loop Forever:
    input_val = r_pot.value                         # Read the potentiometer's current value (16-bit)
    r_led.duty_cycle = Gamma_Correct(input_val)     # Set the red LED's duty-cycle (16-bit) to the gamma corrected pot value
    time.sleep(0.1)                                 # Do nothing for 100 milliseconds
🔴
https://learn.adafruit.com/led-tricks-gamma-correction
https://learn.adafruit.com/led-tricks-gamma-correction