013 - ESP32 MicroPython: UART Serial in MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in today's video we are going to learn another hardware peripherals of microcontroller which is a uart serial in micro python uart is useful for interfacing devices such as df-player mini mp3 player module or a sim 800l gsm module or an hc-06 bluetooth module and etc which basically needs serial communication as mode of control esp32 has three hardware uart serial ports available which are port 0 port 1 and port 2. port 0 is connected to gpio 1 and 3. it is generally used to communicate with the esp32 for flashing and during the reset event in micro python it is reserved for the depot repo in this development board which i have it is accessible through the general purpose breadboard pins this is usable when you disabled the default serial repo and use the web repo instead port 1 is connected to gpio 9 and 10 it is unused but it is usually used for spi plus memory access in this version of esp-32 development board it is not routed to any general purpose breadboard pins lastly the port 2. it is connected to gpio16 and 17. it is unused and it is routed to the general purpose breadboard pins the uarts tx and rx pin can be reassigned to other gpio pins by setting the rx and tx parameters in uart object creation or in object init initialization later on i will demonstrate to you on how to do that for the hardware part i used a usb to serial converter i am using this opto isolated module but any usb to serial converter should work the rx pin of usb to serial converter is connected to gpio17 the tx pin of usb to serial converter is connected to gpio16 with our dedicated hardware serial tx and rx pins of uart port 2. the bcc pin of usb to serial converter is connected to esp32 3.3 volts while the ground pins are commonly connected for more details regarding the circuit connection you may refer to the circuit diagram posted for the software part uart is a class under the machine module so let's import it by calling import machine now let's create an object named uart in small letters and let's use the ur class from machine module using machine that uart in capital letters in its parameters let's just write 2 this parameter is the uart port id number for esp32 it can be 0 1 and 2 only let's set the baud rate to 115 200 bits per second now our uart object is now ready let's open a serial terminal in my case i will use a termite serial terminal make sure that the correct com port number is selected by going to the device manager right click device manager and in device manager click the ports and under the ports com 3 is the usb to serial converter while comport is the esp32 uart bridge so now let's set the country n115 by clicking the settings button in under the port select country and in the baud rate select 115 200 baud rate i also use the append cards return and line feed select ok first let's query our uart object by sending uart it means that our uart object is created a uart class number two is the port id number this is the baud rate set to 115 200. it uses 8 bits for its character with no parity with one stop bit tx pin is set to 17 this is the depot rx pin is set to 16 gpio16 rts is set to negative one it means rts is not used and also the cps buffer is set to 256 and the rx buffer is the same timeout is equal to zero is the time out in milliseconds to wait for writing or reading the first character timeout card is set to 1 is the time out in milliseconds to wait between characters while writing or reading to transmit data use uart dot write let's say hello let's send and as you can see hello string is displayed in the termite serial terminal to receive a data let's send a message from the termite serial terminal let's say hi how do you do let's send now we can query it first using uart that ini and it returns 20 characters which is the number of characters available in the serial received port which could include the line feed and the carriage return characters and to receive it just type uart that read and as you can see this is the message from the termite serial terminal now let me demonstrate to you reassigning the uart rx and tx pin to other pins other than the dedicated pins let me transfer the rx pin from gpio16 and gpio17 to gpio5n18 which is just in the right side of the esp32 let me transfer it okay first let's query again the uart so that we have the record for comparison and as of now it is still in 16 and 17 gpio so now let's call the uart that init function let's set the rx to pi equal to 5 and the tx equal to 18 hit enter and let's query the uart and as you can see the tx and rx pin is chains or reassigned to other pins let me clear this purse and let's test it by sending uart that right let's say hello from esp32 okay it's working now let me revert back to the previous pin assignment which is the depot now for example number one let's try to use our current knowledge of uart to create something in hardware let's say we control the state of the onboard led by sending a command let's import the machine module import machine let's create an led object we are going to control using the uart commands using led is equal to machine that pin which is connected to gpio2 let's set the pin direction using the machine that pin that out now let's create the uart object using the uart class of the machine module uart is equal to machine that uart using port number two with a baud rate of one hundred fifteen thousand two hundred and let's just use all other default settings let's create a global variable here let's name it string message equal to blank here we can create a main loop using the while true inside the main loop let's check if there is data available in the serial using the uart that any is more than zero and if it's true let's store the characters to string message string message is equal to uart that read let's also print the received characters for debug or demo purposes so print string message now we can also check if the received characters contain a command by checking the specific command let's say string on using the if on in string message and if it's true let's turn on the onboard led using led.on let's reply using the you are that right let's say turning turning on led let's also print in the report the same let's also check the op command let's say l if off in string message let's turn up the led we reply to the uart let's say turning off led let's also print in the reform else if no command string file let's just reply uart dot write invalid command that's also the print to the riffle let's save it in computer 1 st013 underscore e x 0 1 let's call it simple uart that pie let's run it and let's see if we can control the led state using uart command line eight oh if let's save again and let's run and let's see as of now the onboard led is off let's send the command on what happen i will check the pins maybe it's not connected properly i just sorted it out i just found out that my dupont wires are loose and no more reliable i end up removing the breadboard and use another set of dupont wires which are female to female dupont wires let's run it and see if we can control the onboard led using a serial command so let's try to send an on and on string and as you can see the onboard led turns on let's try to turn it off by sending off okay and as you can see it replies also in the serial terminal and in the repo let's turn it on again on nice off okay on again okay i hope you learned something now just for fun let's have a simple assignment so here it is in the else part of the if statement here i want you to reply and print the actual command sent for example if i send another command let's say execute i want you to extract the execute string so now when i send execute the reply is invalid command i want you to extract the string execute so that we can display it please do comment your answer please don't be shy my solution could be found in the companion blog post of this video so yeah that's all for now if you have any question regarding this tutorial you may write your inquiry in the comment box provided and if you enjoyed this video please give me thumbs up by clicking the like button and share this to your friends so that it can reach more people who might benefit from this and if you are not yet subscribed please do subscribe now you might also like to visit my blog post attack to tinker that blogspot.com for more details and references such as circuit diagram and source code thank you see you in the next video bye
Info
Channel: TechToTinker
Views: 19,362
Rating: undefined out of 5
Keywords: uart in micropython, serial in micropython, micropython uart, micropython serial, how to use serial in micropython, esp32 uart micropython, esp32 serial micropython, esp32 micropython, how to assignment uart pins in micropython, softwareserial micropython, micropython softwareserial, soft uart micropython, micropython soft uart
Id: Zr01NIP1klc
Channel Id: undefined
Length: 18min 1sec (1081 seconds)
Published: Thu Oct 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.