# 🧩 1.2 Blink and Read

% ```{rubric} 🧩 1.2 Blink and Read
% ```

```{contents}
:depth: 2
```

## 🔰 Tutorial

Learn how to write MicroPython scripts and use a Raspberry Pico W microcontroller to send commands to an RGB LED and read data from a light sensor.

<img src="../../_static/sdl-demo/green-led.jpg" width=35%>

*Flashing the RGB LED on the Maker Pi Pico base with a green color.*

### Getting Started with the Pico (Optional)

Read the [Getting Started with the Pico](https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico) tutorial, some which overlaps with prior content. Note that the onboard led for the Pico W is accessed via `onboard_led = Pin("LED", Pin.OUT)` instead of `onboard_led = Pin(25, Pin.OUT)`, which is for the original Pico. For the "Use digital inputs and outputs", "Control LED brightness with PWM", and "Control an LED with an analogue input" sections, you can still *run* the code without the optional hardware (button, wire-lead LED, resistor, potentiometer, breadboard, M-M jumper leads), though you won't be able to observe the effect.

### Onboard LED

```{note}
The onboard LED is distinct from the RGB NeoPixel LED that you will be using later in the module.
```

The script in this section controls the onboard LED (see black callout below).

<img src="./images/picow-onboard-led.png" alt="Pico W onboard LED with callout" width=300>

✅ Copy and paste this code into a new file (e.g., `onboard_blink.py`) in Thonny and run it to see the onboard LED blink every two seconds.

```python
from time import sleep
from machine import Pin

onboard_led = Pin("LED", Pin.OUT)  # Pico W is slightly different than Pico

while True:
    onboard_led.toggle()
    sleep(2)
```

### Onboard Temperature

Below is a script for reading the Pico W's onboard temperature sensor.

✅ Copy and paste this code into a new file (e.g., `onboard_temp.py`) in Thonny and run it to see the temperature in degrees Celsius.

```python
import machine

adcpin = 4
sensor = machine.ADC(adcpin)


def ReadTemperature():
    adc_value = sensor.read_u16()
    volt = (3.3 / 65535) * adc_value
    temperature = 27 - (volt - 0.706) / 0.001721
    return round(temperature, 1)


T = ReadTemperature()
print(T)
```

### NeoPixels

✅ Read the MicroPython documentation for [Controlling NeoPixels](https://docs.micropython.org/en/latest/esp8266/tutorial/neopixel.html)

✅ Review [MicroPython time module](https://docs.micropython.org/en/latest/library/time.html)

✅ Review intro of [MicroPython Pin class](https://docs.micropython.org/en/latest/library/machine.Pin.html)

### AS7341 Light Sensor

✅ Read the overview page of the [AS7341 Guide](https://learn.adafruit.com/adafruit-as7341-10-channel-light-color-sensor-breakout/overview) and the [Pinouts page](https://learn.adafruit.com/adafruit-as7341-10-channel-light-color-sensor-breakout/pinouts)

✅ Browse the [`as7341_sensor` MicroPython Module](https://github.com/sparks-baird/self-driving-lab-demo/blob/7b9a2a9bc4a5079b03907ffd5c758094217d3872/src/public_mqtt_sdl_demo/lib/as7341_sensor.py)

### Definitions

**GPIO (General-Purpose Input/Output) Pins**: GPIO refers to the pins on a microcontroller or single-board computer that can be configured as either inputs or outputs to interact with external hardware. These pins can be used to read digital signals (input) or send digital signals (output). GPIO pins are versatile and can be used for various tasks, such as reading sensors, controlling LEDs, or interfacing with other digital devices.

**I2C (Inter-Integrated Circuit)**: I2C is a communication protocol used for connecting and exchanging data between electronic components on a circuit board or between different devices. It stands for Inter-Integrated Circuit and allows multiple digital devices to communicate with each other using a common set of wires, typically two (SDA for data and SCL for clock), making it useful for connecting sensors, displays, and other peripherals in a simple and standardized way.

**RGB Color Values**: In the context of RGB color representation, 0 to 255 refers to the range of values for each of the three color channels: Red (`R`), Green (`G`), and Blue (`B`). Each channel can have a value between 0 (no intensity) and 255 (maximum intensity), allowing for a total of 256 different levels of intensity for each color. By combining different levels of intensity for these three channels, you can create a wide range of colors by specifying how much red, green, and blue are mixed together. For example, RGB(255, 0, 0) represents full red, RGB(0, 255, 0) represents full green, and RGB(0, 0, 255) represents full blue. Mixing these channels in different proportions produces various colors in the RGB color space.

**Spectrophotometer**: A spectrophotometer is a scientific instrument used to measure the intensity of light at different wavelengths in a sample. It works by passing a beam of light through the sample and measuring how much light is absorbed or transmitted. Spectrophotometers are commonly used in chemistry, biology, and physics to analyze the concentration of substances in a solution, study the properties of materials, and determine the color or composition of samples by measuring their absorption or emission spectra.

### Additional Resources

- [Introduction to Raspberry Pi Pico guide](https://projects.raspberrypi.org/en/projects/introduction-to-the-pico)
- [Raspberry Pi Pico using onboard Temperature Sensor](https://www.halvorsen.blog/documents/technology/iot/pico/pico_temperature_sensor_builtin.php)
- [What is Stemma QT?](https://youtu.be/6GXRRuFuFy0)
- [Rob Hamerling's AS7341 MicroPython Library](https://gitlab.com/robhamerling/micropython-as7341)
- [Maker Pi Pico GitHub Repository](https://github.com/CytronTechnologies/MAKER-PI-PICO)

## 🚀 Quiz

::::{tab-set}
:sync-group: category

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/351407/assignments/1286927?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/367721/assignments/1499672?display=full_width
:::

:::{tab-item} Sp/Su 2026
:sync: sp2026

https://q.utoronto.ca/courses/437793/assignments/1730325?display=full_width_with_nav
:::

::::

## 📄 Assignment

::::{tab-set}
:sync-group: category

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/351407/assignments/1286928?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/367721/assignments/1499673?display=full_width
:::

:::{tab-item} Sp/Su 2026
:sync: sp2026

https://q.utoronto.ca/courses/437793/assignments/1730326?display=full_width_with_nav
:::

::::
