Back

Activity 2.3: Ohm's Law and Meet the Microcontroller

Electronics Primer — Day 3 of 3

divider

Activity 2.3

Ohm's Law and Meet the Microcontroller

Key Concepts

Ohm's Law

Where the 3.3 V Goes

A Pin Your Code Controls

Code Becomes a Voltage

Your Meter Already Knew

Your resistor read about 1.4 V.

A breadboard2.2's build: a resistor and an LED between 3V3 and GNDMeter probes on either end of the resistor

Today that reading becomes a current.

Ohm's Law

Ohm's Law: the voltage across a resistor equals the current through it times its resistance.

The Ohm's Law triangle: V on top, I and R on the bottom, with a times sign between them

Ohm's Law

The Ohm's Law triangle: V on top, I and R on the bottom, with a times sign between themCircle V: V equals I times RCircle I: I equals V divided by RCircle R: R equals V divided by I

Cover the one you want. The other two tell you how.

Your Resistor's Current

About 1.4 V across 330 Ω. Solve for I.

Value Trace
I = V ÷ R
= 1.4 ÷ 330
= 0.0042 A
= 4.2 mA

Not the 10 mA from 2.1's simulator. Where did the rest go?

The LED Took Its Share

2.2's circuit as a loop: 3V3, a 330 ohm resistor, an LED, GNDAbout 1.4 volts across the resistor and about 1.9 volts across the LED: 3.3 volts in total

Only the resistor's 1.4 V pushes current through its 330 Ω — so about 4 mA flows.

A Pin Your Code Controls

A GPIO pin (general-purpose input/output) is a pin your code sets to 3.3 V or 0 V.

The RedBoard with GPIO 28 highlighted on the top header

Pin 28 replaces yesterday's always-on 3V3.

Meet Your First Object

from machine import Pin
led = Pin(28, Pin.OUT)
led.value(1)

led is an object: a name for pin 28 that you give commands to.

led.value(1) sets it HIGH: 3.3 V.

Code Becomes a Voltage

led.value(0)
The multimeter reading 0.00 volts on pin 28
led.value(1)
The multimeter reading 3.29 volts on pin 28

Unit 1 changed words on a screen. This changes a voltage you can measure.

Too Fast to See

led.value(1)
led.value(0)

Does the LED light?

Too Fast to See

led.value(1)
time.sleep(1)
led.value(0)

It did light — for a few millionths of a second.

time.sleep(1) holds it on long enough to see.

Today's Objectives

  • Calculating current, voltage, or resistance with Ohm's Law
  • Explaining where a circuit's voltage goes
  • Explaining what a GPIO pin is and its two states
  • Writing code that controls a real LED

Key Terms

Ohm's Law
Voltage equals current times resistance: V = I × R.
GPIO Pin
A general-purpose input/output pin that code can control.
HIGH
A pin set to 1: on, at 3.3 V.
LOW
A pin set to 0: off, at 0 V.
Object
A named thing in code that holds data and accepts commands.
Method
A command an object accepts, like value().

'F' → Fullscreen

divider

Build

Start from yesterday's build. Have your Activity 2.2 sheet out: Task 1 needs your resistor reading.

Unplug the USB cable whenever you change the wiring.


Task 1: Your Resistor's Current

  • Find your resistor reading from Activity 2.2, Task 4. If you don't have one, use 1.4 V.
  • Use I = V ÷ R to find the current through your 330 Ω resistor. Show the formula, the numbers, and the answer in mA.
  • Activity 2.1's simulator said 3.3 V across 330 Ω is 10 mA. In one sentence, explain why your real current is smaller.
  • Then solve the two problems on your sheet, showing your work.
The Ohm's Law triangle: V on top, I and R on the bottom

Task 2: Move One Wire

With the USB cable unplugged:

  1. Take out the red jumper from the + rail to a8, and the wire from 3V3 to the + rail.
  2. Run a wire from GPIO 28 to a8. Pin 28 is on the top header.
  3. Plug in the USB cable. The LED stays dark — nothing has told pin 28 to turn on.
  4. Measure the pin: red probe in e8, black probe on the − rail. Record the reading.
A breadboardThe rewired build: an orange wire from GPIO 28 to a8, the resistor b8 to b12, the LED in c12 and c13, and the black jumper to the minus rail, which goes to GNDProbes in e8 and on the minus rail
The RedBoard with GPIO 28 highlighted on the top header

GPIO 28, on the top header. The numbers are pin names, not positions.


Task 3: Your First Code

  • In Thonny, start a new file and type the program below. Save it to This computer as 2-3-first-light.
  • Press Run. The LED lights.
  • Measure e8 again, the same way as Task 2. Record the reading.
  • In Thonny's Shell at the bottom, type each line below and press Enter. Watch the LED after each one. If the Shell won't take them, add them to the end of your file and press Run instead.
2-3-first-light
from machine import Pin
led = Pin(28, Pin.OUT)
led.value(1)
Shell
>>> led.value(0)
>>> led.value(1)

Task 4: Blink Once

  • Add the three new lines. Run it: the LED lights for one second, then goes out.
  • Change the 1 to 0.1, then to 3. Run each, and write down what time.sleep()'s number means. Then put it back to 1.
2-3-first-light
from machine import Pin
import time
led = Pin(28, Pin.OUT)
led.value(1)
time.sleep(1)
led.value(0)

Task 5: Break It on Purpose

Two changes, one at a time. Record what happens, and undo each one before the next.

  1. The wrong pin. Run this version, with 29 in place of 28. Does the LED light? Is there an error? Then measure pin 29 itself: push a jumper into the pin 29 socket on the top header, red probe on it, black on the − rail.
2-3-first-light — wrong pin
from machine import Pin
led = Pin(29, Pin.OUT)
led.value(1)
  1. No sleep. Delete the time.sleep(1) line and run it. What do you see?
2-3-first-light — no sleep
from machine import Pin
import time
led = Pin(28, Pin.OUT)
led.value(1)
led.value(0)

Neither one is an error. Both programs run to the end and Thonny reports nothing. Say what each one actually did.

divider

Checkpoint

  • Your current is worked out with the formula and the numbers, in mA
  • Pin 28 read about 0 V before your code, and about 3.3 V after
  • Your LED lights for one second, then goes out
  • Both Task 5 results are recorded, including the pin 29 reading
divider

Reflection

Answer the following questions before submitting your work.

  1. Show how you calculated the current through your resistor from your Activity 2.2 reading, and explain why it was less than the 10 mA the 2.1 simulator showed.
  2. Before your code ran, pin 28 read about 0 V; after led.value(1), about 3.3 V. Explain what led.value(1) actually does to the pin.
  3. With Pin(29, Pin.OUT), the program ran with no error, but the LED stayed dark. Explain why the computer couldn't tell you anything was wrong.
divider

Submit

Submit your 2-3-first-light program file and your completed Activity 2.3 sheet.

Activity Complete