C8: Arduino Basic + Sensor Interfacing

Aim: Make Arduino control an LED, a buzzer, and a fan based on a sensor reading (example uses an LDR light sensor). You will simulate this in Tinkercad first, and learn the safe wiring to use on a real board.

Requirements (virtual/hardware):

  • Tinkercad account (for simulation) or Arduino Uno, breadboard (hardware)

  • LDR (Light Dependent Resistor) on analog input A0

  • LED + 220 Ω resistor

  • Active buzzer (or piezo)

  • Small DC fan or motor

  • NPN transistor (e.g., 2N2222) or MOSFET (e.g., IRLZ44) and 1 kΩ base/gate resistor

  • Flyback diode (1N400x) for motor

  • Jumper wires, external power supply for fan if needed

  • Arduino IDE (for real hardware)

Wiring — step-by-step (Tinkercad & Real world)

1) LED (simple, direct pin drive)

  • Parts: LED, 220 Ω resistor.

  • Connections:

    • LED long leg (+) → 220 Ω resistor → Arduino pin 9 (digital/PWM).

    • LED short leg (−) → GND.

  • Why: LED current is limited by 220 Ω so pin can safely drive it.

2) Buzzer (active buzzer recommended)

  • Active buzzer (simple on/off):

    • Buzzer + → Arduino pin 8.

    • Buzzer → GND.

  • Passive buzzer requires tone() and may need a transistor if current draw is high.

3) DC fan / small motor (use a transistor driver)

Do not connect a motor directly to an Arduino pin. Use an NPN transistor or MOSFET and a diode.

  • Parts: Motor/fan, NPN transistor (2N2222 or TIP120), 1 kΩ resistor, diode 1N4007.

  • Connections:

    • Motor + → +5V supply (or external battery +V).

    • Motor → Collector of NPN transistor.

    • Emitter → GND.

    • Arduino fanPin (e.g., pin 3, PWM) → 1 kΩ resistor → transistor base.

    • Place 1N4007 diode across motor terminals (cathode to +V, anode to transistor collector) to protect from spikes.

    • Common ground: Arduino GND must connect to battery/external supply GND.

  • Why: transistor switches motor current; diode absorbs voltage spikes.

Tinkercad Simulation Steps (practical)

  1. Open Tinkercad → CircuitsCreate new circuit.

  2. Drag an Arduino Uno and a breadboard.

  3. Add LDR, LED (+ resistor), buzzer, and a DC motor.

  4. Wire LDR: one leg to 5V, other leg to A0 and to a 10kΩ resistor to GND (voltage divider).

  5. Wire LED to pin 9 via 220 Ω. Buzzer to pin 8. Motor to transistor as above (Tinkercad has a motor component).

  6. Add Serial Monitor in your code to print sensor values.

  7. Start simulation, move the LDR light slider, and watch outputs change.

Example Arduino Code (complete, commented)

// Outputs example: LED, Buzzer, Fan controlled by LDR on A0

const int ldrPin = A0;      // analog input

const int ledPin = 9;       // PWM-capable pin for LED

const int buzzerPin = 8;    // buzzer on/off

const int fanPin = 3;       // PWM pin driving transistor controlling fan

int threshold = 400;        // example threshold (0-1023)

 

void setup() {

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

  pinMode(buzzerPin, OUTPUT);

  pinMode(fanPin, OUTPUT);

  digitalWrite(buzzerPin, LOW);

  analogWrite(fanPin, 0);

}

 

void loop() {

  int ldrValue = analogRead(ldrPin); // 0 (dark) to 1023 (bright)

  Serial.print(“LDR: “);

  Serial.println(ldrValue);

 

  if (ldrValue > threshold) {      // bright -> turn outputs ON

    digitalWrite(ledPin, HIGH);    // LED ON

    digitalWrite(buzzerPin, HIGH); // Active buzzer ON

    analogWrite(fanPin, 200);      // Run fan at ~78% (0-255 scale)

  } else {                         // dark -> outputs OFF

    digitalWrite(ledPin, LOW);     // LED OFF

    digitalWrite(buzzerPin, LOW);  // Buzzer OFF

    analogWrite(fanPin, 0);        // Fan OFF

  }

 

  delay(200); // small delay for stability

}

 

Notes on code:

  • analogRead() reads LDR value. Calibrate threshold for your setup.

  • analogWrite() uses PWM to control motor speed (when using a transistor/MOSFET).

  • Use tone(buzzerPin, frequency) / noTone() if using passive buzzer for melodies.

Safety and Practical Tips

  • Never power a motor directly from an Arduino pin. Use a transistor or motor driver.

  • Use a flyback diode across motors to protect electronics from voltage spikes.

  • Use a separate power supply for high-current devices (motors), but always connect grounds together.

  • Check datasheets: make sure the buzzer or motor current does not exceed transistor or supply limits.

  • Use resistors (220 Ω) for LEDs to limit current.

  • On a breadboard, ensure solid contacts; loose wires cause intermittent behavior.

Troubleshooting

  • LED doesn’t light: check polarity (long leg = +), resistor value, pin number in code, GND connection.

  • Buzzer silent: verify active vs passive type; try tone() for passive. Check pin/wiring.

  • Fan not spinning: ensure transistor wired correctly (base resistor present), motor +V connected, diode placed, and common ground. Test transistor with LED or smaller load first.

  • Flickering outputs: loose wires, weak supply, or noisy sensor readings; add a small delay and secure connections.

Extension Ideas 

  • Add hysteresis to threshold logic so outputs don’t flip rapidly when sensor is near threshold (e.g., turn ON at 420, OFF at 380).

  • Use multiple sensors (e.g., LDR + temp) and control outputs differently (if dark and hot → turn fan on).

Make the buzzer beep in intervals instead of continuous tone when triggered (use millis() timing).

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