Let's experiment with the PiicoDev OLED Module! We'll connect our OLED to a Raspberry Pi Pico and run some examples for drawing shapes, text, fancy graphs and we'll even bring in a motion sensor for a fun remix at the end.

Transcript

I'm going to show you how to get started with a PiicoDev OLED today using a Raspberry Pi Pico. We'll connect these two together, get some example code working to see what's possible, and then we'll take a dive through all the functions, making text, shapes, lines, plots, and even animations.

Let's get started. To follow along, you'll of course need a Raspberry Pi Pico with the pin soldered facing down, a PiicoDev OLED module and expansion for Raspberry Pi Pico, and a PiicoDev cable to connect everything together.

Start by connecting your Pico to the expansion board, making sure the USB connector is on the same side as the 2-pin battery connector. Connect the PiicoDev cable and connect the other end to the OLED module. And I've mounted everything to this PiicoDev platform to keep it nice and stable. And finally, connect your Pico to your computer with a USB cable.

Find the download section for this article, and you'll find three files that we need to download. Right-click each link and select Save Link As. I'm going to save these to a PiicoDev directory in My Documents. We're going to be using Thonny for this tutorial. If you haven't used Thonny before, check out our guide for getting started with the Raspberry Pi Pico.

Open Thonny, connect to your Pico, and select all three files, so I'll left-click the top file, hold down Shift, left-click the bottom file, right-click, and upload. We can see the three files are on our Pico. Open up main.py, and this is ready to run, so we can just press Ctrl D to run the script.

We have some text with a counter running, and then some bar graphs filling up the screen from left to right. We can see that we're plotting two lines across the screen now. And finally, a bouncy square demo. You can see every time it hits a wall, this number increases by one.

These OLEDs are tricky to film, so you might see some glitches from the camera, but in person, it looks super smooth. Alright, so that's a quick rundown of everything you can do with the PiicoDev OLED using a Raspberry Pi Pico.In this video, my screen is flickering. That's just how the camera is filming the OLED. Yours won't do this. Now, this feature demo is just to show off what's possible with the OLED.

Let's wind things back and do a few simple examples. We'll start with the line example. Copy and paste this into the main file that's on your Pico, and run. You can see we get this horizontal line up here, and then this vertical line, and then this diagonal line in sequence.

Let's have a look at the code. We do the normal PiicoDev imports and setup, and then we create an H line, which is a horizontal line. We create a V line, which is a vertical line, and then a generalized two-point line. For H line, these arguments are the starting point for XY, the length of the horizontal line, and the color. We usually use one for the color, which means white.

The PiicoDev OLED module is a 128 by 64 pixel display. Pixels are addressed by XY coordinates, starting in the top left with X increasing to the left and Y increasing downwards. That means our top left pixel is 0, 0, and our bottom right pixel is 127, 63. That means that this starting point 10, 10 is 10 pixels in from the left and 10 pixels down from the top. The horizontal line is then drawn 80 pixels long. And the same with the V line. We start at the same point and draw 35 pixels down. Finally, we draw a two-point line where we join the endpoints of these two horizontal and vertical lines. So we connect the endpoint of the vertical line with the endpoint of the horizontal line. And that creates our right angle triangle on the display.

When we call these functions to create shapes on the screen, they don't appear immediately. To actually update the physical display with this new information, we call display.show.

Moving along, we can draw some rectangles. Copy the rectangle example and paste it into Thonny.And give the script a run.

Now, what looks like one rectangle and one hollow rectangle is actually three. This first rectangle, we draw the left-hand rectangle, which is unfilled. And so that is just display.rect. We have the starting points for the top left of the rectangle, which is at 10, 10, just like our lines from the last example, followed by a width and a height. So we can see 20 pixels wide and 50 pixels tall. It's going to be a taller, skinnier rectangle. And that's what we see.

Next, we draw a filled rectangle. We have some start points. We have some width and height. And here we have an additional argument, which is the fill color. In this case, one for white. We then draw a filled rectangle inside the first filled rectangle. So this picture frame shape here with this thick white line is actually a white rectangle with a black rectangle drawn over it. And of course, we need to update the display with display.show.

Now to remix the code, let's take this second filled rectangle, the black rectangle, and move it to the right by 10 pixels. Let's just see what happens. We'll turn this 60 into a 70 and run the script. And you can see now our rectangle has drawn off the edge of the other one. And we've created this C shape by shifting that black rectangle to the right.

Of course, we can print text onto our module, grab the text example, paste that in and run it. And you can see we have a few lines of text. We have, hello world, this is me. And then some numbers. We have 123.4567 and 123.46.

In our first call to display text, we display the literal string, hello world, at 00 with a color of one. And so this text is being printed from the top left corner. That 00 is the top left corner of the text. And so we can just put our string directly into that function. In the next call to text, we are printing something called my string.And that is just some variable that's set to "this is me". So we can print literal strings. We can print variable strings.

In the next line, we're printing a number. We call text with my number, and this is "123.4567". Now, because this is a number, we have to convert this to a string before we can print it. And finally, the last call is a formatted print statement where we print my number again, but with a formatted string. This syntax is for printing a floating-point number with two decimal places.

And you'll notice that in each one of these calls, the Y coordinate is increasing by 15. And that's what gives us our new lines of text.

It's possible to create graphs with our OLED module. Find the graph example and copy that in. I'll paste that over everything and run it. And you'll see that we have two beautiful sine waves being plotted across the screen. Look at that.

As usual, we need to call the regular PiicoDev imports, create our display. And then this time we need to create a graph object. A graph object allows you to push data to it, and it will step that data across the display. Because we have two lines that we're graphing, we need two independent graph objects.

Graph one, we create a graph 2D object. And then we set some arguments, the min value and max value. These set the scales of the uppermost and lowermost points on the graph. So that means that if we plot a 1, a value for 1, it will be at the top of that graph.

And we create another graph object, graph 2, which has different min and max values. So it's going to be at a different scale. In the loop, we assign two variables to some trigonometric functions. Here we have a sine wave with an amplitude of 1. Really, the amplitude is the important part here. So Y is a sine wave with an amplitude of 1, and Z is a cosine wave with an amplitude of 2.

Because we're animating this, we need to erase the previous plots that we've made. So we use the clear method of each of our graph objects. And then we push new data to the graph objects. We push Y and Y*0.5 to graph one, and we push Z to graph two.

And then we use the update method of each graph object, which will redraw the plot with the new animation frame. So that's how you make a graph with our OLED module.In this tutorial, we are shown how to graph two sine waves on an OLED module. The code loops through the two sine waves, updating the x values each time. Additionally, an "H" line is plotted along the y-axis, which represents 0 on the graph. Display.show is called at the end, and because graph 2 has a larger amplitude, it takes up more space on the OLED module.

The article also includes a tutorial on using a PiicoDev motion sensor to control the position of a rectangle on the OLED display. By reading the angle from the motion sensor, an X and Y position can be calculated for the rectangle. This allows the user to move the rectangle by rotating or tilting the motion sensor.

Overall, both tutorials illustrate the power and versatility of OLED modules in DIY electronics projects.Y coordinate, and then just call rect with those X and Y coordinates. Simple.

And there you have it, a bunch of cool things you can do with the PiicoDev OLED module. If you make anything cool out of this starter project, or if you just have some questions, let us know on our forums.

We're full-time makers and here to help. Until next time, thanks for watching.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.