ESP32 MicroPython OLED Tutorial with ADC & FTP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this micro Python tutorial will demonstrate how to connect and drive a color o LED display using an ESP 32 it'll also touch on using ADC pins and FTP file transfers here's an SSD 135 1 128 by 128 pixel color o LED display connected to an DSP 32 I wrote a simple brick game similar to the arcade classic Arkanoid I'm using a touch-sensitive linear potentiometer connected to an ADC pin to control the paddle I wrote the game and the display driver is completely in micro Python it's difficult to film OLEDs my video doesn't do justice to this high-quality display which is flicker free very bright and has excellent contrast in addition it's small energy efficient and can be powered with 3.3 volts this is my fourth tutorial in my micro Python ESP 32 series please check out my earlier videos which demonstrate the basics neopixels sensors mqtt web servers and more my videos are fast paced but my website contains complete notes for all my tutorials a link will be placed in the description this 1.5 inch SSD one three five one module uses a spy interface which I recommend over I squared C because it's faster the board requires five GPIO pins to communicate with the ESP 32 in addition to VCC and ground the SSD 135 one ground in is connected to a ground on the ESP 32 the VCC pin is connected to a 3.3 volt pin the SCL pin is connected to GPIO 18 on the SP 32 which is the clock pin for the hardware V spy bus SDA is connected to GPIO 23 which is the mossie pin for the same beast by bus r ESO reset is connected to GPIO 16 DC which toggles between data and command mode is connected to GPIO 17 CS which is chip select goes to GPIO 5 there are currently 2 available hardware spy hosts on the ESP 32 H spy and V spy V spy which I'm using here allocate spend 18 for clock pin 23 for mossie and pin 19 for my so since the display is Right only my so is not needed each spy uses 14 13 and 12 for clock mossie and my so respectively there are two other spy buses but they're currently reserved for the core system this may change in the future on a breadboard I have the SSD 135 one oh le d-- module and a we most lowland 30 to the ground from the SSD 135 one is connected to a ground on the ESP 30 to the VCC pin is connected to a 3.3 volt pin SCL is connected to GPIO 18 SDA is connected to GPIO 23 reset to GPIO 16 DC the GPIO 17 and cs2 GPIO five currently the CS pins are software controlled only in micro Python so you can choose any available GPIO pins for this video I'm using a Raspberry Pi 3 running the latest version of raspbian stretch to communicate with the lowland 32 which has the latest firmware build of micro Python installed please watch the first video in the series to learn how to load the ESP 32 firmware and install our shell at the time of filming this video the only micro Python SSD 135 one driver I could find was by Adafruit unfortunately it had very limited functionality and slow performance therefore I wrote my own open-source library which is available in my github site SSD 135 1pi is the main LED library I've included several examples to demonstrate how to use the library the breakout game I showed earlier is also included along with several fonts images game levels and utilities I'll scroll up and copy the gate clone address to the clipboard then switch to a terminal window and type git clone and paste in the address from the clipboard this copies all the files from a github repo to the raspberry pi LS shows the downloaded micro Python SSD 13.1 folder CD into the new folder LS again shows the library contains several files and folders in my last video I used our shells our sync command to copy multiple files to any SP 32 a couple of viewers complain that our shell is really slow for copying which is true therefore in this video I'll use FTP which is much faster there's an excellent lightweight micro Python FTP server created by Robert HH the library FTP pi is compatible with the ESP 32 again I'll copy the git clone address to the clipboard switch back to the terminal CD back to my home directory clear and type git clone and paste in the address from the clipboard LS shows the downloaded esp8266 FTP server folder despite the name it does work with the ESP 32 CD into the downloaded folder LS shows the files the FTP file needs to be copied to the ESP 32 so I'll run our shell LS / PI board shows the contents of the ESP 32 I've already uploaded the main dot PI file for my last video which automatically runs it boot and connects to my Wi-Fi network access is required for FTP communications CPE FTP PI / Pi board copies the FTP file to the ESP 32 if you're on a Windows or Mac computer you can also use out of fruits MP utility they transfer the FTP file LS / PI board shows the FTP file in the root folder of the SP 32 type repple to enter the repple again if you're on Windows you can use putty on a Mac you can use the built in screen program to access the repple import ftp starts the ftp server on the SP 32 at IP address 192 168 1 92 10 we can now connect to the FTP server at any FTP client and start transferring files on the PI in a new terminal type sudo apt-get install FTP this installs a very basic Linux command line FTP client type FTP followed by the ESP 32s IP address currently authentications not supported so just press Enter we're now logged in on the left side of the screen the FTP server knowledge is the connection LS lists the file contents of the ESP 32 boot pi ftp pi and main dot pi exclamation point LS lists the file contents of the current folder on the Raspberry Pi which is the home directory LCD which stands for local change directories used to switch into the micro Python SSD 135 1 folder exclamation point LS shows the contents of the library input start . pi is used to copy all the Python files to the SP 32 ok I don't want a confirmation prompt for each file so I'll cancel the copy with ctrl C type prompt to toggle the confirmation prompts off input start period PI again and this time it's silent the files are copying the video is not sped up the FTP server does a great job of quickly transferring the files LS shows all the copied Python files on the ESP 32 exclamation point LS shows that there are still three folders that need be copied fonts images and levels unfortunately this simple FTP client isn't very efficient for copy and folders so let's try a more advanced client typing by closes the FTP connection and also causes the SP 32 to terminate the FTP server since the FTP server has already been imported it can be restarted by typing FTP FTP server the FTP server is back up on the same IP address on the PI in a blank terminal type sudo apt-get install FileZilla FileZilla is a free full-featured graphical FTP client which runs on Linux Mac and Windows after installation from the Raspberry Pi main menu click internet FileZilla then click file site manager new site I'll call it ESP 32 for host type the ftp server IP address next switch to the transfer settings tab and click passive transfer mode and check the box to limit the number of connections to 1 click connect which connects to the server very quickly the contents of the ESP 32 are splayed in the right pane on the Left pane are the file contents of the raspberry pi home folder scroll down and widen the file name column double-click micro Python - SSD 135 1 to switch to that folder ctrl-click is used to highlight the fonts images and levels folders right-click and select upload to transfer the 3 folders and their contents to the ESP 30 to the lower pane shows the upload status 36 files and the three folders are being copied in addition to the improved speed ftp is support by many of the popular code editors such as atom sublime and vyas code this allows you to write code in your favorite editor and have the files automatically uploaded to the ESP 32 the 3 uploaded folders are now on the SP 32 and the software is ready to go back in the repple type import OS then OS lister can be used to list the directory contents using python there are several demos that you can use to help get started ok now let's take a look at drawing to the display we'll start with the included shapes demo from SSD 1 351 display and color 565 are imported from machine pin and spire imported by is instantiate Adan bus number two which is V spy the baud rate is set to 14.5 million you can go higher but you may get some noticeable artifacts on the screen it's necessary to specify the clock and mossy pins an SSD 135 1 display is instantiated and pass the spy along with the pins for DC ECS and reset display is now ready to take drawing commands I've documented my o LED library thoroughly in addition to providing several demos the library has support for most common geometric primitives such as circles ellipses lines points polygons and rectangles draw us for an outline shape and fills for a solid object will run the shapes demo by typing import demo shapes a blue screen is drawn using the clear command with the optional color parameter the screen is cleared in a few lines in a rectangle are drawn in different colors draw lines can take a list of coordinates to create any 2d shape fill polygon creates a solid heptagon some more rectangles a triangle again using draw polygon a couple of circles and ellipses clean up turns off the display and clears the spy resources my library supports up with 65535 different colors which is referred to as RGB 565 color space let's take a look at the color palette demo the HSV to RGB method from my previous video help smoothly cycle through the colors XY 4 loops span the displays width and height to create an 8x8 grid the spectrum is divided into 64 colors the color 565 method takes the RGB values returned from the HSV to RGB function and generates the corresponding 16-bit color fill circle draws a solid radius 7th circle using the specified color the loop repeats and the grid is filled next there's an animation example called demo bouncing boxes this demo uses the micro Python random and Newton libraries there's a box class upon initialization the random numbers are seated and the boxes assigned nonzero random XY speeds between negative 5 & 5 seating the random number generator using the method tik CPU ensures different random numbers each time the programs there's a method to update the position and speed another method handles drawing boxes there's six different colored boxes six different sizes a list holds the six boxes at the start of an infinite while loop takes us stores the current milliseconds of for loop cycles through the boxes updates their position and draws them at the end of the loop ticks TIFF is used to calculate the duration and then sleep us attempts to adjust the loop to 30 frames per second which is a good speed for animation sprites and images can be loaded from the ESP 32 flash storage they need to be formatted in RGB 565 for off format I created a Python command line utility called IMG to RGB 565 an editor to my repos utils folder it can convert most of the common image types such as jpg and pink to the required format the demo sprite program is similar to the bouncing boxes demo but instead of rectangles it animates a sprite drawing an object using points lines and shapes is relatively slow a sprite is a store image that can be displayed much faster which is necessary for animating more complex graphics the load sprite method loads an RGB 565 raw image of a micro Python logo as before there's an update position and a draw method the draw sprite method draws the sprite XY are the top-left corner it's whr the width and height sprites are great for animation but for fixed graphics images are more efficient the demo images program uses the draw image method to load image files an RGB 5 6 500 format from the ESP 32 flash drive and draw them directly to the o LED display unlike sprites you don't need a buffer variable to store the images which saves memory again forgive my photography because the images really do look great in person flicker free dark blacks and good color contrast with my SSD 135 won library I've included a module with support for XG LCD fonts in both portrait and landscape mode you convert any TrueType font to XG LCD format using a free utility called G LCD font creator there's instructions for the conversion process on my website the demo fonts program first imports the XG LCD font library next 9 different fonts are instantiated arcade pics balli Broadway Express oh dalsu fixed font neato Robotron Eunice face and Wendy I've included these 9 fonts in my repo draw text is used to draw text in the different fonts with different colors draw text also accepts an optional landscape parameter to allow text in landscape mode as opposed to portrait which is the default in landscape mode the XY coordinates still refer to the characters top left draw text can also accept a background color parameter so you can set the text background color in addition to the actual text color not show it as an optional spacing parameter to change the letter spacing there's also a measure text method which we use to help position your text I'll put some sample code on my website now let's take a look at the game arcade games like arkanoid use the spinner to control the paddle I didn't have any extra spinner so I'm using a spectra symbol linear 10k ohms soft pot it functions similarly to a standard potentiometer pressing down along the pad changes the resistance if you don't have a soft pot you can substitute any regular potentiometer it could be hooked up exactly the same one of the pots terminal is connected to ground the other is connected to 3.3 volts in series with a 6,200 ohm resistor by default the ESB 32 ADC pins can only read up to around 1 volt I use the ADC attend method to set the attenuation to 6 DB which increases the range to about 2 volts a potentiometer is a variable voltage divider when its terminal ends are connected to a power supply moving the wiper varies the voltage on the middle pin without the 6200 ohm resistor this would be 0 to 3.3 volts since the ADC can only read 2 volts this would lose over a third of the pots range the resistor offsets the pots range from 0 to about 2 volts this takes advantage of the pots full range the wiper is connected to GPIO 36 which is labeled VP an optional resistor is placed in series a 6.8 PF cap is placed between the wiper and ground to help smooth out some of the noise the SP 32 ADC is not very good it's very noisy and there are issues with linearity I plotted the ADC values between 0 and 2 volts the green indicates the ideal line the orange is the actual values there's a significant distortion towards both ends however most of the path is relatively straight it's linear enough for a game paddle there's just a little roughness when the paddle is close to the walls if you were trying to read a sensor you can use a polynomial equation to smooth out the readings to improve accuracy you could also increase the VCC terminal resistor and add one to the ground terminal this could crop the voltage to the linear portion of the Charter black on the breadboard a resistor is connected to a 3.3 volt pin on the ESP 32 the other end is patched to one of the terminals on the soft pot I'm using a 4,700 ohm resistor instead of a 6200 because it was the closest I could find in my parts bin this puts the output around 0 to 2 point to 5 volts which only sacrifices a little bit of range an optional 1000 ohm resistor is connected to GPIO 36 the other end is patched to the soft Potts wiper the other terminal in the soft pot is connected to the breadboard ground rail then the ground rail is connected to a ground on the ESP 32 let's see how the ADC is working so far as mentioned earlier the ESP 32 ADC is very noisy which can lead to erratic readings currently in the game paddle is tracking terribly this could be mitigated with a little math and some sampling but a quick fix is just to add a 6.8 PF capacitor between the ADC pin and ground now that's more like it the paddle is now tracking my finger very smoothly it's good enough for gameplay if you need more precision I made an earlier Raspberry Pi video on external ADC chips such as the mCP 3002 and the ATS 1 1 1 5 I'll put a link in the description here's the code to read the soft pot from machine import a DC and pin an ADC is instantiated on GPIO 3 6 afterwards the attend method is set to 6 TB which increases the default 0 to 1 volt range to 0 to 2 volts the ADC read method can then be used to read the Potts position the analog voltage is translated to a 12 bit value between 0 and 4095 where 0 equals 0 volts and 4095 equals 2 volts the results are divided by 44 and incremented by 5 to match the game paddles x-axis range of 6 to 98 pixels I created 9 levels for the game the binary level files are comprised of three bytes XY in color I place the code I used to generate them in the repose utils folder in case anyone wants to add more levels I hope you found this video helpful you can support this channel by leaving a like sharing and subscribing I really appreciate all the positive comments and thanks for watching
Info
Channel: rdagger68
Views: 20,989
Rating: undefined out of 5
Keywords: ESP32, MicroPython, Raspberry Pi, SSD1351, OLED, ADC, FTP, Lolin32, Wemos, Arkanoid, SoftPot
Id: a7DrFqqu-78
Channel Id: undefined
Length: 18min 4sec (1084 seconds)
Published: Wed Dec 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.