Raspberry Pi Pico: OLED Display (SSD1306)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks my name is tim and welcome back to my channel so in today's video we're going to be looking at these things these are tiny little oled display modules they're pretty inexpensive and really really useful now you might want to wire one of these things up to a project simply for the purposes of debugging even if it's not going to be a permanent part of the project long term so let's take a look at how you use a little oled display like this with the raspberry pi pico these displays are built around the ssd1306 oled display driver and you can find its datasheet online now this driver is 128 by 64 pixel dot matrix style driver with the necessary display ram and oscillator built in making it very easy to use however displays built using it come in a number of sizes and the ones i have here are only 128 by 32 pixels and obviously you can pick different size displays to suit your particular application the ssd 1306 runs on 3.3 volt logic natively but most displays have an on-board regulator making them compatible with both 5 volt and 3.3 volt microcontrollers though it's probably a good idea to confirm that this is true for your display before powering it up now these displays come in two varieties one using the sbi interface and we've looked at how to use spi previously and one using the i squared c interface and again we've also covered i squared c in a previous video both types of display use the same controller and the only difference is how you need to wire up the interface a software driver is provided by micro python and this supports both interface variants so it doesn't really matter very much which one you're using today i have the i squared c version and i'm wiring it up to the pico's first i squared c interface as follows the scl or clock line is connected to gpio pin 17 physical pin 22 the sda or data line is connected to gpio pin 16 and physical pin 21 the ground connection is at physical pin 23 on the pico and finally the vcc or voltage inline is connected to the pico's 3.3 volt output at physical pin 36 and that's it my dodgy soldering notwithstanding this is a very simple module to wire up so let's take a look at the software side of things so the first thing we need is the ssd1306 driver and you can find this in the micropython repository under drivers display and i'll leave a link to this file in the description to use it just load it up in thoni and go to save as then select the pico and save the file as ssd1306.py okay with the driver installed let's look at a very simple demo this program begins by importing the necessary modules to create the pin object and the i squared c interface object we then go ahead and import the driver that we just prepared now i'm using the i squared c version of this display so i'm importing ssd1306 underscore i2c if i was using an spi based display though i would import ssd1306 underscore spi the same software package provides both drivers so with our needed modules imported we go ahead and define two variables the first variable here w has 128 in it and that's the width of our display the second variable h has 32 in it and that's the height of the display module that i'm using now obviously if your display is a different size you can change these values as needed so with our width and height defined we can go ahead and create the i squared c interface object that we'll be using so we do that here and we say we'd like to use the first i squared c interface we're using pin 17 as our clock line pin 16 is our data line and we provided the desired frequency we'd like to operate at once we've created the i squared c interface we can then go ahead and use it to scan for our peripheral and retrieve the i squared c address that it's been assigned right with the i squared c object created the address retrieved and the width and height defined we can go ahead and build an instance of the ssd 1306 i squared c driver and we're going to store that in a little variable called oled and that's all there is to setting up this program now for the simple demo we're just going to write out the text string raspberry pi pico ssd1306 the driver uses an 8 pixel by 8 pixel mono space font for its characters meaning we can fit at most 16 characters on a line so the first function here just clears the display by setting all the pixels to black we then write out the first half of our text because we can't fit it all into the width available so we pass in to this text function the raspberry pi string that we want to render and we pass in an x and y pixel position now this pixel position is relative to the top left hand side of the display so we're basically saying please begin drawing text five pixels in from the left and five pixels down from the top then we'll need to make a second call to the text function to render the second part of our string and we're going to do that one line below so here we say please draw 5 pixels in from the left and 15 pixels down from the top these draw calls won't actually update the display immediately these are basically buffered internally in order to refresh the display we need to call this dot show method here now when we call that all the previous draw commands that we've made since the last time we called dot show will take effect and the screen will be updated so we need to ensure that we call dot show every time we want to actually change what's displayed okay so let's run this program now and see what our display looks like all right well that's looking very good so now that we have something on our display let's play about a bit here i'm connecting up the little thumbstick that we first looked at in the adc interface video and you can refer back to that video if you would like to know more about how to use the picos adc interface and this thumbstick in particular here i'm just wiring up the thumbstick to the adc interface number one and number two okay so with our thumbstick connected let's take a look at this second demo program well it's initialized exactly the same as the first but this time we have two new adc objects here for adc interface one and two and of course these have the axes of our thumbstick connected we then have this little loop here and this loop is basically querying the adc interfaces to get back a value for the x and y axis of the thumbstick it then uses this little bit of maths here to convert the values from the range of the adc interface into a range that fits within the dimensions of our display so let's take a look at how this is done well we get back a value from the adc which ranges between 0 and 65 535 that's a 16 bit value so if we divide that value by 65 535 we basically convert it into a value between 0 and 1 as a floating point we can then use that zero to one range as a multiplier or a scalar to a value here which in this case is width minus 10. so if you imagine we get 65 535 back that's the far extremity of the adc interface we divide that by 65 535 to get back one we then multiply the width minus 10 by 1 we get back well the width minus 10. so when the thumb stick is all the way across on the x axis we'll get back a value which is equivalent to the full width of our display minus 10 pixels now that 10 pixel subtraction is basically just to establish a margin in fact we're establishing a 5 pixel margin because if we have a value of 0 from this calculation we increase it by five so that we'll never go less than five and we reduce the maximum width by 10 so that we end up accounting for the five pixels we added and another five pixels to give us a 5 pixel boundary all the way around our display and of course we're just going to do the same thing again for the y axis so now as we move the thumb stick between the x and y maximum and minimum extents we should end up with positions that range between 5 and the full width or height -10 so once we've got those positions we can just blank our screen and then we write out a single character and in this case i'm using an asterisk and the position that we're going to place it at well that's going to be the x and y value that we just got back from the previous calculation and then of course we need to call oled dot show in order to actually update the screen so if this is all working properly we should be able to move this character around on the screen using our thumbstick so let's see if it works well that's certainly working nicely very good there you go hopefully you've been able to see that these little modules are really really easy to use and they're very handy little things to have knocking around in your parts box so hopefully this has been interesting and maybe even helpful thank you for watching and i'll see you next time
Info
Channel: Tinker Tech Trove
Views: 27,991
Rating: undefined out of 5
Keywords:
Id: YSqGV6NGWYM
Channel Id: undefined
Length: 11min 30sec (690 seconds)
Published: Tue May 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.