Skip to content

Instantly share code, notes, and snippets.

@VeggieVampire
Last active December 29, 2022 08:14
Show Gist options
  • Save VeggieVampire/a606797b339694cc75bc0edd43489bbb to your computer and use it in GitHub Desktop.
Save VeggieVampire/a606797b339694cc75bc0edd43489bbb to your computer and use it in GitHub Desktop.
This code creates an instance of the RtlSdr class, which represents an RTL-SDR device, and sets the center frequency of the device to 101.1 MHz using the center_freq property. It also sets a step size for the frequency changes in Hz. Next, the code initializes Pygame and creates a window using the pygame.display.set_mode() function. It also sets…
import rtlsdr
import pygame
import time
# Create an instance of the RtlSdr class
sdr = rtlsdr.RtlSdr()
# Set the center frequency of the RTL-SDR device (in Hz)
sdr.center_freq = 101.1e6
# Set the sample rate of the RTL-SDR device (in Hz)
sdr.sample_rate = 2.4e6
# Set the gain of the RTL-SDR device (in dB)
sdr.gain = 'auto'
# Set the bandwidth of the RTL-SDR device (in Hz)
sdr.bandwidth = 2.4e6
# Set the step size (in Hz) for frequency changes
step_size = 100e3
# Initialize Pygame
pygame.init()
# Set the window size
window_size = (400, 300)
# Create the window
window = pygame.display.set_mode(window_size)
# Set the window title
pygame.display.set_caption("RTL-SDR Signal Detection")
# Set the background color
bg_color = (0, 0, 0)
# Set the signal color
sig_color = (0, 255, 0)
# Run the event loop
running = True
while running:
# Check for key events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
# Increase the center frequency by the step size
sdr.center_freq += step_size
elif event.key == pygame.K_DOWN:
# Decrease the center frequency by the step size
sdr.center_freq -= step_size
# Read IQ samples from the RTL-SDR device
samples = sdr.read_samples(1024)
# Calculate the power spectrum of the samples
power_spectrum = abs(samples)**2
# Check if the signal strength is above a certain threshold
if power_spectrum.mean() > 100:
# Signal is detected
sig_detected = True
else:
# Signal is not detected
sig_detected = False
# Clear the window
window.fill(bg_color)
# Draw the center frequency on the window
text = f"Frequency: {sdr.center_freq / 1e6:.3f} MHz"
font = pygame.font.Font(None, 36)
text_surface = font.render(text, True, (255, 255, 255))
window.blit(text_surface, (10, 10))
# Draw a rectangle on the window to indicate the presence of a signal
if sig_detected:
pygame.draw.rect(window, sig_color, (10, 50, 380, 240))
# Update the window
pygame.display.update()
# Quit Pygame
pygame.quit()
@VeggieVampire
Copy link
Author

also
wget https://github.com/roger-/pyrtlsdr/archive/master.zip
unzip master.zip
cd pyrtlsdr-master
python3 setup.py install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment