When setting the time, the day of the week is irrelevant. On the day of the week, it should be noted that Monday is 0 and Sunday is 6. (year, month, day of the month, day of the week, hours, minutes, seconds, microseconds). The time tuple is structured a little differently: If you want to set the date and time of the ESP32, then you have to use the RTC class. Here’s what the output looks like: Output of local_and_unixtime.py The RTC class (year, month, day of the month, hours, minutes, seconds, day of the week, day of the year) “localtime” is used as a tuple in the format: Now_secs = time.time() # secs since, 00:00 Now = time.localtime() # query local time as tuple You can calculate the frequently used Unix time from this. With the time module, you can query the current time and the seconds since, 00:00 o’clock. Here is a small example: import timeĭelta = time.ticks_diff(time.ticks_ms(), start_ms) With ticks_diff() you can determine time spans. Millis() and micros() are called ticks_ms() and ticks_us() in MicroPython. In addition, you can use sleep() which takes seconds as parameter. The equivalents to delay() and delayMicroseconds() when using ESP32 with MicroPython are sleep_ms() or sleep_us(). In my measurements, the range was 0.086 to 3.18 volts instead of 0 to 3.3 volts (see here). The drawback: the output voltage is linear to the value passed to dac.write(), but the slope of the straight line is not quite right. Here’s how it works: from machine import Pin, DAC You can pick off a real analog signal between 0 and 3.3 V at pins 25 and 26. However, due to the timer frequency of the ESP32 (80 MHz), you only get the full 10 bit resolution up to the following PWM frequency:įor example, if you choose 20 MHz as frequency, you only have a resolution of 4, i.e. Other functions are: eq() # get current frequency Pwm16 = PWM(Pin(16)) # create PWM object from GPIO 16 Here is an example: from machine import Pin, PWM If you program the ESP32 with MicroPython, only 10 bit resolution is available (0-1023). With the Arduino implementation of the ESP32 you can set the resolution (see here). To produce pulse width modulation (PWM), you create a PWM object for a specific pin and pass the frequency and duty cycle. In my own measurements, the A/D converter overflowed in the maximum range at about 3.15 volts. This data is from the MicroPython Quick Reference. With adc.atten(ADC.ATTN_XDB) you set the attenuation and thus determine the input voltage range: Parameter selection for attenuation Alternatively, you can set with adc.width() 11, 10 or 9 bits. In order to use a pin as an A/D converter input, you first create an object. Now for the code: from machine import ADC, PinĪdc.atten(ADC.ATTN_11DB) # ATTN_11DB, ATTN_6DB, ATTN_2_5DB, ATTN_0DB (default)Īdc.width(ADC.WIDTH_12BIT) # WIDTH_12BIT (default), WITDTH_11BIT, WIDTH_10BIT, WIDTH_9BIT They generate quite a bit of noise, and what’s worse, they are not linear. You would define a pin for reading a button state like this:īuttonPin = Pin(18, Pin.IN, Pin.PULL_DOWN) # button Pin = GPIO18 Read the ADCs of the ESP32 with MicroPythonįirst of all: As powerful as the ESP32 is, as bad are its A/D converters. In addition, you can connect internal pull-up or pull-down resistors if they are available at the respective pin. You can switch the pin using pinname.value(0/1). You can read the GPIO level with pinname.value(). Whether a pin acts as input or output is determined with Pin.OUT or Pin.IN. The big difference to C++ is that the pins are defined as objects. A simple blink program on the ESP32 with MicroPython looks like this: from machine import Pin Since I covered switching of GPIOs in detail in the last post, there is only a compact repetition here. Short repetition: Switching and reading the GPIOs The only difference to the pin assignment in the Arduino implementation is the position of the I2C pins. The pinout is: Pinout of the ESP32 with MicroPython I have explained the pins of the ESP32 and their functions in detail here. I will only very briefly discuss Wi-Fi and Bluetooth.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |