Build Your Own YouTube Subscriber Counter with a Raspberry Pi Pico W
Build Your Own YouTube Subscriber Counter with a Raspberry Pi Pico W
Hey there, tech tinkerers! Want to make a cool gadget that shows off your YouTube subscriber count in real-time, complete with a little celebratory jingle when you gain a fan and a sad tune when you lose one? You’re in the right place! This project uses a Raspberry Pi Pico W, a small LCD screen, and a buzzer to create a fun desktop counter. Don’t worry if you’re new to this—I'll guide you step-by-step, and by the end, you’ll have something awesome to show off.
Here’s what we’re building: a device that checks your YouTube subscriber count every 8 seconds, displays your channel name and count on a 1602 LCD, and plays a tune on a piezo buzzer when your numbers change. Let’s dive in!
What You’ll Need
- Raspberry Pi Pico W ($6–$10): The Wi-Fi-enabled version of the Pico. (The regular Pico won’t work here—it needs Wi-Fi!)
- 1602 LCD Display with I2C ($5–$8): A 2-line, 16-character screen. The I2C version uses fewer wires.
- Piezo Buzzer ($1–$2): A little speaker for our tunes. Passive ones work fine.
- Jumper Wires: A handful to connect everything.
- Computer with USB Port: To program the Pico.
- Wi-Fi Network: For the Pico W to connect to the internet.
- Google API Key: Free to set up—we’ll cover this carefully.
- YouTube Channel ID: We’ll find this together.
Total cost? Around $15–$25 if you’re starting from scratch. Not bad for a custom gadget!
Step 1: Set Up Your Hardware
First, let’s wire everything up. Don’t stress—this is simpler than it sounds!
Piezo Buzzer:
- Positive (+) pin to GP15 on the Pico W.
- Negative (-) pin to GND. (Check your buzzer—some have a + marking; if not, test both ways.)
1602 I2C LCD:
- SDA to GP0.
- SCL to GP1.
- VCC to 3.3V (not 5V—the Pico uses 3.3V logic).
- GND to GND.
Double-check your connections, and you’re good! No soldering needed—just plug the wires into the Pico’s pin holes.
Step 2: Install MicroPython on Your Pico W
The Pico W runs MicroPython, a lightweight version of Python. Here’s how to get it set up:
- Download the MicroPython UF2 file for the Pico W from micropython.org.
- Plug your Pico W into your computer via USB while holding the BOOTSEL button.
- A drive called RPI-RP2 will appear. Drag the UF2 file onto it.
- The Pico will reboot, and the drive will disappear. Done!
Now, grab a tool like Thonny (free at thonny.org) to write and upload code. In Thonny, set the interpreter to “MicroPython (Raspberry Pi Pico)” under Run > Configure Interpreter.
Step 3: Get Your Google API Key and YouTube Channel ID
This part trips up beginners, so let’s go slow and steady. We need two things from Google and YouTube.
Getting Your Google API Key
Go to Google Cloud Console:
- Visit console.cloud.google.com.
- Sign in with your Google account (the one tied to your YouTube channel).
Create a Project:
- Click the project dropdown at the top (it might say “Select a project”).
- Hit New Project. Name it something like “YouTube Counter” and click Create.
Enable the YouTube API:
- From the left menu, go to APIs & Services > Library.
- Search for “YouTube Data API v3” and click it.
- Hit Enable.
Create an API Key:
- Go to APIs & Services > Credentials.
- Click Create Credentials > API Key.
- A key (a long string like AIzaSy...) will pop up. Copy it and save it somewhere safe!
Optional Security: For a demo, you can use the key as-is. For long-term use, click Restrict Key, select “YouTube Data API v3,” and save.
Finding Your YouTube Channel ID
Open YouTube:
- Go to youtube.com and sign in.
- Go to Settings:
- Click your profile picture (top-right) > Settings > Advanced Settings.
Locate Your Channel ID:
- You’ll see a “Channel ID” like UCxxxxxxxxxxxxxxxxxxxxxx. Copy it!
- If you don’t see it, go to your channel page (e.g., youtube.com/@YourName), view the page source (right-click > View Page Source), and search for channelId. It’ll be in a line like "channelId": "UC...".
Got both? Great! Keep them handy for the next step.
Step 4: Add the Code
We’ll use two files: one for your secrets (Wi-Fi and API stuff) and one for the main program.
Secrets File (secrets.py)
In Thonny, create a new file and paste this:
# secrets.py wifi_ssid = "YOUR_WIFI_NAME" wifi_password = "YOUR_WIFI_PASSWORD" google_api_key = "YOUR_API_KEY_FROM_GOOGLE" channel_id = "YOUR_CHANNEL_ID_FROM_YOUTUBE"
Replace the placeholders with your Wi-Fi name, password, API key, and channel ID. Save it as secrets.py
and upload it to your Pico W (File > Save As > Raspberry Pi Pico).
Main Program (main.py)
Now, the fun part! Create another file and paste this:
# main.py import network import urequests import time from machine import Pin, PWM, I2C from pico_i2c_lcd import I2cLcd from secrets import wifi_ssid, wifi_password, google_api_key, channel_id # Setup I2C for LCD i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) I2C_ADDR = i2c.scan()[0] # Find LCD address lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) # Setup buzzer buzzer = PWM(Pin(15)) # Connect to Wi-Fi wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(wifi_ssid, wifi_password) # Wait for Wi-Fi max_wait = 10 while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: break max_wait -= 1 print("Waiting for Wi-Fi...") time.sleep(1) if wlan.isconnected(): print("Wi-Fi OK!") lcd.putstr("Wi-Fi Connected") else: lcd.putstr("Wi-Fi Failed") while True: time.sleep(1) time.sleep(2) lcd.clear() # Play a tune def play_tune(notes, durations): for note, duration in zip(notes, durations): if note > 0: buzzer.freq(note) buzzer.duty_u16(1000) else: buzzer.duty_u16(0) time.sleep(duration) buzzer.duty_u16(0) # Happy tune for gaining subscribers celebratory_notes = [659, 784, 1047, 784] # E5, G5, C6, G5 celebratory_durations = [0.2, 0.2, 0.2, 0.2] # Sad tune for losing subscribers sad_notes = [392, 349, 330, 294] # G4, F4, E4, D4 sad_durations = [0.3, 0.3, 0.3, 0.3] # Fetch YouTube data def get_youtube_data(): url = f"https://www.googleapis.com/youtube/v3/channels?part=statistics,snippet&id={channel_id}&key={google_api_key}" try: response = urequests.get(url) data = response.json() count = data["items"][0]["statistics"]["subscriberCount"] name = data["items"][0]["snippet"]["title"] response.close() return int(count), name except: print("API Error") return None, None # Main loop last_count = None lcd.backlight_on() while True: count, name = get_youtube_data() if count is not None and name is not None: lcd.clear() lcd.move_to(0, 0) lcd.putstr(name[:16]) # Show channel name (max 16 chars) lcd.move_to(0, 1) lcd.putstr(f"Subs: {count}") if last_count is not None: if count > last_count: play_tune(celebratory_notes, celebratory_durations) elif count < last_count: play_tune(sad_notes, sad_durations) last_count = count else: lcd.clear() lcd.putstr("API Error") time.sleep(8) # Update every 8 seconds
Save it as main.py
. You’ll also need two LCD libraries:
pico_i2c_lcd.py
and lcd_api.py
: Grab these from this GitHub repo or search online for “pico i2c lcd library.” Upload both to your Pico W.
urequests
: Usually built into MicroPython, but if it’s missing, download it from micropython-lib and upload it.
Step 5: Run It!
In Thonny, upload all files (secrets.py
, main.py
, pico_i2c_lcd.py
, lcd_api.py
, and urequests.py
if needed) to your Pico W. Click the green “Run” button on main.py
. Your LCD should light up, connect to Wi-Fi, and start showing your channel name and subscriber count. Gain a subscriber? Hear a happy tune. Lose one? A sad melody plays.
It updates every 8 seconds—perfect for a quick demo! (For long-term use, you might slow it to 5 minutes with time.sleep(300)
to save API calls.)
Troubleshooting
- LCD Blank? Check wiring or run an I2C scanner script to confirm the address.
- No Sound? Swap buzzer pins—it’s easy to mix up positive/negative.
- API Error? Double-check your API key and channel ID.
You Did It!
Congrats—you’ve built a YouTube subscriber counter from scratch! It’s a fun way to learn coding, electronics, and APIs. Tinker with the tunes or timing to make it your own. Got questions? Drop them in the comments—I’d love to help. Now go show off your creation!
Written by Never Code | Visit My Site
Comments
Post a Comment