C9: Arduino / RPi Pico Advanced + Sensor Interfacing

Aim:
To take a messy piece of code and improve it for clarity, reliability, and maintainability.

Requirements:

  • Arduino IDE or Thonny IDE

  • Arduino Uno or Raspberry Pi Pico

  • Multi-sensor setup (e.g., LDR + LM35 temperature sensor)

  • A sample buggy code (given by teacher)

Procedure (Step-by-Step)

  1. Examine the Given Code

    • Read through the messy code carefully.

    • Identify unclear variable names, repeated logic, and missing explanations.

  2. Add Comments

    • Write a short note above each important section explaining what it does.

Example:

cpp
CopyEdit
// Reading temperature from LM35 sensor

int tempValue = analogRead(A0);

  •  
  1. Organize into Functions

    • Break the code into smaller functions such as:

      • setupSensor() → to initialize sensors

      • readTemp() → to get temperature data

      • controlLight() → to turn LED ON/OFF based on conditions

  2. Test the Code

    • Upload your cleaned code to the microcontroller.

    • Run and check if it behaves as expected.

  3. Debug and Improve

    • If it doesn’t work, use Serial Monitor or print statements to see where it’s going wrong.

    • Remove any unnecessary lines and make the code more efficient.

  4. Final Review

    • Ensure that the code works in all test cases.

    • Check if it’s readable for someone new to the project.

Fill in the form for S’O’A Fablab Orientation 2021.