ESP32 MicroPython Tutorial with Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this tutorial will demonstrate how to setup and program a TSP 32 microcontroller running micro Python from a Raspberry Pi all my tutorials are fast paced but the code notes updates and more are available my website and as always a link will be placed in the description here's an ESD 32 breakout board connected to a 16 by 2 LCD display showing temperature readings from a DES 18 B 20 sensor connected VL 1 wire interface this is a standalone set up but ESP 32s can easily transmit temperature and other sensor data wirelessly from multiple remote locations to a central server such as a raspberry pi here's the JQ 6500 sound module connected via you are - an ESP 32 the module lets you store and play audio files a motion detector is also connected to a GPL input pin when motion is detected and interrupts fires and a sound plays the ESP 3 - is a very powerful and versatile chip here's a generic ESP 32 breakout board and I got an eBay for a few dollars there are other varieties such as the Wii Maus lowland 32 which I just ordered for an upcoming video because it has a lithium battery interface with charging they're sold on Aliexpress for around the same price as my generic eBay version Adafruit also has an ESB a 32 board with battery support called the foods a 32 it's part of their feather series it's more extensive but they offer a large selection of add-on boards with features this is a real-time clock SD card displays GPS and more another board is the PI column why PI 2.0 which is compact as an RGB LED and has an external antenna option the ESP 32 is designed for efficient Wi-Fi and Bluetooth including Bluetooth Low Energy or ble that's powerful 240 megahertz dual core microcontroller with 520 K of SRAM the chip is designed for mobile devices so it has ultra-low power consumption there are 32 GPIO pins of support for I squared C I 2's spy and you are in addition has multiple analog to digital channels and digital to analog hardware accelerated encryption pulse width modulation capacitance touch interfaces and a bunch more the built-in Wi-Fi makes the sp3 to a great solution for wrote devices such as sensors inputs and relays this interest I'll probably turn this video into a series that demonstrates connecting sensors to the SP 32 in detail and using network messaging protocols such as mqtt to report data back to the PI wirelessly one of the main reasons I like the ESB 3 - is that you can program it in Python more specifically micro Python the very efficient streamlined version of the Python 3 programming language which is optimized to run on microcontrollers such as the SP 32 the Raspberry Pi is great for many projects but it's a full fledged computer with a Linux operating system for simple repetitive tasks such as monitoring sensors and controlling relays it's often easier cheaper more reliable and more efficient to use a micro controller which boots up instantly and just runs your program especially if your project uses batteries or solar because the ESP 32 uses a fraction of power required by the pi also many sensors are susceptible to interference especially on longer wire runs more reliable approach is to connect the sensor with a short wire to an ESB 32 and then transmit the results using Ethernet or a wireless protocol to get started the micro Python firmware needs to be uploaded to the ESB 32 this can be done with a simple USB cable which provides a two-way serial interface between the pie and the SP 32 it also provides 5 volt power on a breadboard I have an ASB 32 breakout board next to the board is an old raspberry pi of D plus but any PI should work I'll plug one end of the USB cable into the PI the other end plugs into the microUSB port on the ESD 32 board the red light indicates that it's got power now for the software first please make sure the PI is up-to-date from a terminal type sudo apt-get update and sudo apt-get upgrade should use the latest version of raspbian jesse to ensure you have all the necessary software a utility called ESP tool will be used to upload the micro Python firmware it's installed using sudo pip 3 install ESP tool I'm using tip 3 to target Python 3 okay that's successfully installed now type D message grep TTY USB this shows that a CP - 10 X you are - USB bridge so attach the port TTY USB 0 some devices need to be put into programming mode using the push buttons on the board before you can use the SB tool however I found it's not necessary on the ESP 32 at least not on this one to test the connection type ESP tool PI double - port slash dev slash TTY USB zero followed by the command flash ID this queries the ESP 32 for basic info such as the chip type DSP 32 and flash size four megabytes looks like everything's okay before uploading the firmware it's recommended to erase the chip i'll use the up-arrow to recall to last ESP tool command and change flash ID to erase flash this gives us a blank slate okay the chip erase completed successfully a copy of the micro Python firmware is required you could build it yourself from the source code on the github repo but it's much easier just to download a daily build open a web browser and go to micro Python org click the download tab click ESP 32 currently only 1 firmware is available for the SP 32 click to download it once that completes close the browser the micro Python firmware build was downloaded to the Downloads folder LS shows a long file name highlight the name and copy it to the clipboard I recall the last ESP tool command this time erase flash has changed to write flash hex 0 specifies the starting address to write type download slash for the Downloads folder and then just paste them the firmware file name from the clipboard the firmware is now being written to DSP 32 it's pretty quick but I'll speed it up so you don't have to wait it verified ok so the board should be ready for coding micro Python boards have a read eval print loop or rebel which is a simple interactive programming environment it's similar to the Python idle shell on the SP 32 the repple is accessed using a serial connection other boards like the esp8266 which is a very popular predecessor to DSP 32 in addition to a serial rebel also have a web repple which allows you to manage and program them over the web as of this video this feature has not yet been implemented on the ESB 32 along with many other features but new stuff is being added daily so by the time you watch this video it might be more options for now we'll stick to serial the same USB cable that we use the program vs C 32 can also be used to access the rebel any serial program such as putty or screen should work however these programs won't let you manage the p32 file system which can be used to store your programs ideally you want a program that provides a rebel terminal and also can perform file system commands I've tried several programs and currently my favorite is our shell it can be installed using sudo pip 3 install our shell this simple program will run on the Raspberry Pi therefore it's access to the repple terminal on the ESP 32 it also provides file management to transfer manipulate files on both the PI and the e SP 32 installation is complete to start our shell type our shell double dash buffer size equals 30 tak P dev TTY USB 0 ok our shells running is connected to the USB port the current terminal can be used to execute file commands for example type boards to list all connected boards the single board is returned with ID pi board which will need it as a reference later the boot pi files automatically run in startup and contains low-level code to setup the board and finished booting you typically don't want to edit it however you can add a file called main PI if you need to run your own code at startup after the boot PI type ripple to open the micro Python programming environment determine will now accept Python code print hello world outputs hello world let's try something a little more colorful a typical first program is to blink an LED instead of a regular LED I'll use a neopixel LED this is an RGB LED with a built-in shift to control color and brightness it can be driven using a single data line and it's very easy to connect to the ESP 32 a 5 hold pin provides power a 1n 4004 diode is used to drop the neopixel voltage from 5 volts to 4.3 volts which allows me a pixel to read the ESP 32s 3.3 volt data output the SP 32 3.3 volt output each v at least 70% of the neopixel supply voltage to register commands if the needle pixel is running at 5 volts and the SP 32 is below this range at 66% but at 4.3 volts it's good to go at 76% the ground from the SP 32 is connected to a ground on the neopixel GPL 13 is connected to data in please note that a single neopixel can use up to 60 milliamps at full brightness therefore if you want to run a strip of LEDs I'd use an external power source to prevent damaging the board Adafruit has several helpful tutorials regarding neopixels and I'll put links up on my website an ESP 32 breakout board is plugged into a breadboard one problem with this particular breakout board is that it's a little too wide and only one row of pins is accessible in the ESB 32 they do make versions that are narrower and more breadboard friendly the five volt 10 from the SP 32 is connected to a 5 volt rail the ground is connected to a ground rail I'll add the neopixel ground first it's best practice to connect the ground first and when disconnecting the ground should be removed last here's an eight-millimeter neopixel LED it's placed with the flat side on the right so the third pin ground connects to the black ground jumper one in 4000 port diode is placed between the 5 volt rail and the 5 volt pin on the neopixel again the diode has a voltage drop of 0.7 volts which ensures the neopixel can read the ESP 32s 3.3 volt output instead of a dialer you can also use a level shifter on the data line to convert between 3.3 volts and 5 volts finally the data in pin on the neopixels connects the GPL 13 on the ESP 32 okay that takes care of the hardware now let's write some Python code minimize our cell and run idle 3 create a new file from machine pin is imported the pin library is very similar the Raspberry Pi GPIO library lets reference it control DSP 30 to GPIO pins from neopixel import neopixel this library drives neopixel leds and strips from time imports leap micro python is a subset of python therefore it doesn't have all the standard libraries for example an easy way to cycle RGB LED colors is to vary the hue between 0 and 1 this can be done with the HS feed RGB method which is part of the python color sis library since it's not included with micro Python I'll just paste the HSB to RGB function into my code from the colors dis library HSV is a common cylindrical coordinate representation a point in an RGB color model this function simply converts an HSV value Q saturation and brightness to an RGB value red green blue I'm not going to explain how this function works because it's not relevant to this tutorial but all the code is on my website if you want to explore it further a neopixel is instantiated on GPIO pin 13 1 indicates the number of LEDs neopixels are also sold in strips of LEDs and you can control multiple using a single GPIO pin spectrum is a list to represent 2048 colors this first range is 0 to 2048 and the second is 2048 to 0 the try statement to catch errors wraps the main while loop which is infinite a for loop cycles through the color spectrum range he was set to a value from 0 to 1 which is divided into the 2048 steps np0 are first the first neopixel LED you only have one for using a strip that could reference other LEDs the HSV the RGB function is passed the hue 1 for saturation and brightness is set to only 15% to make it a little easier to film MP writes that's the neopixel led to the specified color loop sleeps for 10 milliseconds and continues except is used to gracefully exit the program upon ctrl C finally ensures the LED is turned off by setting red green and blue to zeros okay I'll save the program I'll call it RGB pi and put it in the Documents folder back in our shell I type ctrl X to exit the repple I'm still in our shell LS list the contents of my home directory CD documents switches to the folder where I saved the Python program LS again shows the file RGB PI LS / PI board shows the file contents on the ESP 32 Pi board is the ID for the ESP 32 that we determined earlier using the board's command currently the SP 32 only contains the boot PI file CPE RGB PI / Pi board copies the RGB PI file to the ESP 32 now LS / PI board shows the RGB file was successfully copied repple to return to the micro Python rebel import RGB loads and runs the micro Python program that's on the breadboard the neopixel is fading between colors I need to work on my photographic skills the actual LED colors are much more vibrant than it appears on the video and it's cycling very smoothly please let me know in the comments if you want me to make more ESP 32 videos and if so what you'd like to see you can support this channel by subscribing leaving a like and sharing thanks for watching
Info
Channel: rdagger68
Views: 207,340
Rating: undefined out of 5
Keywords: ESP32, Raspberry Pi, NeoPixel, MicroPython, ESPTool, rshell, WEMOS, Wipy, Huzzah32
Id: w15-EQASP_Y
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Sun Jul 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.