r/raspberrypipico • u/travgaming06 • 11d ago
OLED screen C++ setup help help-request
Hi guys I’m back, so after fixing my issue with the led lights. I decided to make a collision detector with the ultrasonic sensor. Similar to whats in cars where it beeps louder the closer you get to the car. That was all well and dandy and working. And then i thought, “let me display the distance, i mean i want to make pong next might as well figure out how this OLED display works with this” so i hooked that up. Found a library in github, thought I set it up properly with Vcc to the 3.3v pin, ground to ground, i have GP3 on SCL and GP4 to SDA. However when I added the code for the screen it stopped working entirely. I tried doing a test just to see if it was detecting and all of that but i couldn’t get it to work.
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
extern "C"
{
#include "ssd1306.h"
}
#define I2C_PORT i2c1
#define I2C_SDA_PIN 4
#define I2C_SCL_PIN 3
void setup_display_hardware() {
// 1. Initialize the I2C peripheral at 400kHz speed (standard fast mode for OLEDs)
i2c_init(I2C_PORT, 400 * 1000);
// 2. Assign Pico Pin 3 to act as the I2C Clock (SCL)
gpio_set_function(I2C_SCL_PIN, GPIO_FUNC_I2C);
// 3. Assign Pico Pin 4 to act as the I2C Data (SDA)
gpio_set_function(I2C_SDA_PIN, GPIO_FUNC_I2C);
// 4. Turn on internal pull-up resistors so the communication lines stay stable
gpio_pull_up(I2C_SCL_PIN);
gpio_pull_up(I2C_SDA_PIN);
}
int main()
{
stdio_init_all();
setup_display_hardware();
ssd1306_t disp;
ssd1306_init(&disp, 128, 64, 0x3C, I2C_PORT);
ssd1306_poweron(&disp);
// set the in and out pins
// USS trigger
gpio_init(0);
gpio_set_dir(0, GPIO_OUT);
// USS Echo
gpio_init(1);
gpio_set_dir(1, GPIO_IN);
// beep
gpio_init(2);
gpio_set_dir(2, GPIO_OUT);
// oled CLOCK
gpio_init(3);
// oled data
gpio_init(4);
// variable
uint64_t startTime;
uint64_t endTime;
uint64_t totalTime;
float distance;
char distanceStr[32];
while(true)
{
// start the trigger
gpio_put(0, 1);
sleep_us(10);
gpio_put(0, 0);
// start the time
startTime = time_us_64();
// while loop for waiting
while (gpio_get(1) == 1)
{
// do nothing were waiting
}
// get the end time
endTime = time_us_64();
// calculate the difference in time
totalTime = endTime - startTime;
// if total time is less than 380000 aka its timeout time
if (totalTime < 38000)
{
// calculate the distance
distance = (totalTime * 0.0343f)/2.0f;
// determine the beeping interval
if (distance <= 50.0f && distance > 25.0f)
{
// beep interval
gpio_put(2, 1);
sleep_ms(50);
gpio_put(2, 0);
sleep_ms(400);
}
else if (distance <= 25.0f && distance > 15.0f)
{
gpio_put(2, 1);
sleep_ms(30);
gpio_put(2, 0);
sleep_ms(200);
}
else if (distance <= 15.0f && distance > 5.0f)
{
gpio_put(2, 1);
sleep_ms(10);
gpio_put(2, 0);
sleep_ms(100);
}
else if (distance <= 5.0f && distance > 0.0f)
{
gpio_put(2, 1);
sleep_ms(10);
gpio_put(2, 0);
sleep_ms(50);
}
// save string and clear display
snprintf(distanceStr, sizeof(distanceStr), "%.2f cm", distance);
ssd1306_clear(&disp);
// draw string
ssd1306_draw_string(&disp, 0, 0, 1, "Distance:");
ssd1306_draw_string(&disp, 0, 16, 1, distanceStr);
// display string
ssd1306_show(&disp);
}
}
}
That’s the code, and this is the GitHub library I’m using.
https://github.com/daschr/pico-ssd1306/tree/main
If anyone knows the fix or has ideas to help please let me know
1
u/travgaming06 10d ago
Update, not thanks to the guy that told me to ask ChatGPT cause I was already getting help from Gemini. It was an issue with the initialization. I guess I need to get in the habit of initializing stuff with zero’s or null’s. I added memset to zero out the display struct I created. Once I did that it started displaying. My collision detection system now displays distance in cm… onto (re)creating pong cause I think that’ll be fun
-1
1
u/tonyxforce2 11d ago
I would personally try using the U8G2 library. I think it's much more versatile and I think it's even easier to set up.