Latest Entries »

DHT11 module can sense both Temperature and Humidity data.

As a module it has 3 pins .Vcc can be connected to 3.3v , as it can work with 3.3v.

Gnd to Gnd.

Data pin is connected to GPIO 13.

DHT_ESP

DOMOTICZ service is running on Raspberry PI which is on the same WIFI network as my PC.

 

rpi8

 

Domoticz Dashboard can be accessed from a browser by typing in the address followed by port number

192.168.0.150:8080

 

Image 3

Click on Setup –> Hardware

Select Type as DUMMY and provide a name

Image 4

 

Image 5

When you click ADD it appears on the list.

Image 6

Click on Create Virtual Sensors

Select Sensor Type as Temp+Hum

ans provide a name.

Image 7

 

Image 8

 

Click on OK .

The Virtual Sensor is visible under TEMPERATURE tab.

 

Image 11

If you want a description to be added , click on Edit and add description.

 

Image 12

 

Now go to ESP EASY by typing in the IP address 192.168.0.110

Under Controllers select protocol DOMOTICZ HTTP.

Image 13

Provide the IP address of DOMOTICZ Service and the port number.

Click Enabled.

Image 15

 

Under Devies click ADD.

Image 16

Select a device Environment DHT11

Image 17

 

Click Enabled and select the GPIO pin as GPIO 13.

 

Image 18

Interval is the REFRESH rate of DHT11.

DHT11 has a refresh rate of 1 sec , meaning every second it can send out data.

On DOMOTICZ Virtual Sensor you can see the display of Temperature and Humidity.

Image 20

video :

 

blog_image

In the previous post we installed ESPEASY over NODEMCU.

This post is on controlling an LED connected to GPIO 12 of NODEMCU from DOMOTICZ Service.

 

LED_NODE

 

My Raspberry PI and PC both are on same Local Area Network , i.e connected to same WIFI Router .

Domoticz is installed on RPI

 

rpi1

 

 

Image 21

The IP address of RPI connected to the Router over WIfI is 192.168.0.150 & the DOMOTICZ dashboard is accessed by URL

192.168.0.150:8080

 

rpi7

 

On Domoticz dashboard click on Setup –> Hardware

 

Image 28

 

Create a Type  “Dummy “ (select from the drop down list) and provide a name as LED.

Image 29

 

Click on ADD

 

Image 30

Now click on  “ Create Virtual Sensors “

Image 31

Select a Sensor type as SWITCH and provide a name.

 

Image 32

 

The Virtual Switch is visible under SWITCHES tab.

Image 33

Click on EDIT

Against On Action , type in this HTTP command

http ://192.168.0.110/control?cmd=gpio,12,1

192.168.0.110  is the IP address of ESPEASY NODEMCU.

LED is at gpio12 & 1 for switching ON , 0  for Switching Off.

Image 34

Now go to the dashboard of ESPEASY by typing in 192.168.0.110 in the browser.

Under Controllers select a Protocol Domoticz HTTP

Provide the IP address of DOMOTICZ and the port no. as 8080

Click on Enabled & Submit.

Image 35

Under Devices select from the drop down Switch Input –Switch

Provide a Name and click Enabled.

Select the GPIO as GPIO 12.

Image 36

Click on SUBMIT.

Now you can switch On / Off the LED by clicking bulb icon on Domoticz.

 

Image 3

VIDEO:

 

blog_image

ESP EASY IOT OS ON NODEMCU

Video :

 

ESP module can be turned into an IOT MULTIFUNCTION Sensor device by  installing  ESP EASY Firmware on to the module.

For HOME AUTOMATION projects it suits well and can be easily controlled from application servers like DOMOTICZ.

Configuration of the ESP EASY is entirely web based , so once the firmware is loaded , you just need a common web browser to work with.

Older version of ESP EASY is R120. Mega is the newer version.

https://github.com/letscontrolit/ESPEasy/releases

documentation :

https://espeasy.readthedocs.io/en/latest/

 

supported Hardware ESP modules :

https://www.letscontrolit.com/wiki/index.php?title=ESP_Hardware

The NodeMCU or WeMOS D1 mini are recommended , as they have inbuilt USB.

Sensors supported :

https://www.letscontrolit.com/wiki/index.php?title=Devices

Only the list of sensors are supported.You need not write any code separately for sensors.Just connect the Sensor and configure on Dash board. Display or control it from DOMOTICZ over HTTP or MQTT protocols.

Download the MEGA version of ESP EASY.

Under dist folder Flasher application is provided.Double click on that.

Image 1

 

Select the COM port where Nodemcu is connected.

Before selecting a .bin firmware file click on the update button.

Latest .bin files will be downloaded from Github repository.

 

Image 2

Select the relevant .bin file for your hardware.Here I’m selecting ESP8266 4MB version, as my hardware is NODEMCU with 4096 flash memory.

Test beta version supports Nextion display hardware.

Image 3

Setting of WIFI & fixed IP can be done later. For now Flash ESP EASY.

It takes approx 2 minutes to upload.

 

Image 5

Once uploaded , close the flasher and reset Nodemcu.

It starts in AP mode and the WI FI access point name is ESP_EASY.

Connect your PC to this AP using password  configesp

When a fresh ESP Easy module boots up, it has no Wifi config and it will start as an Access Point.

Use a Wifi enabled device (Laptop, Smartphone,..) to search for Wifi access and try to find an access point with the name ESP_Easy.

Connect to this access point with default password:

configesp

 

Image 6

 

Once connected , open your browser and type in ip address   192.168.4.1

You should get a  welcome screen:

Select your SSID and provide password.Click on CONNECT .

Now your NodeMcu connects with your SSID.

If the connection succeeded, you will see a screen, listing it’s new local IP address.

You can now reconnect your laptop or other device back to your private Wifi network.

Then click the “proceed” link to contact the ESP module on it’s private IP address:

Image 7

ESP EASY Nodemcu is offered an IP ADDRESS by your Router.

Use this address on a browser to access the DASHBOARD of ESP EASY.

Under MAIN tab is the SYSTEM INFO.

Image 8

 

Click on CONFIG tab.

Here you can change the AP password from configesp , if you need.

At the bottom is the IP Settings.

You can provide an IP address to make it STATIC.

The IP Address must be in the range of your Gateway IP.

Gateway IP is the IP of your ROUTER.When any client connects with the Router , it provides an IP address by its DHCP method.This IP will be in range of Gateway IP , only the last octet changes.

Finally click SUBMIT to effect changes.

Image 9

Next tab is CONTROLLERS tab.

Under this you select a PROTOCOL .

HTTP,MQTT,UDP all are supported.

In this demo we control an LED from browser commands.So select STANDALONE.

 

Image 10

Under HARDWARE tab you can set any GPIO pin to be High or Low on Boot up.

It is not needed in this demo , so we move on to next tab DEVICES.

Click on ADD from the list of devices.

Select SWITCH INPUT – SWITCH from the drop down.

Provide a name , like LED and then click the ENABLED .

Under GPIO , select GPIO 12 (D6) where we’ve connected an LED.

Click on SUBMIT.

Image 11

Now from your browser use this command to control LED.

Image 12

 

Image 13

Instead of GPIO you can use PWM to control brightness of LED.

gpio, pwm can be in small or capital letters.

Image 14

 

blog_image

 

title_h

DHT11 humidity and temperature sensor is available as a sensor and as a module. The difference between this sensor and module is the pull-up resistor and a power-on LED.

DHT11 is a relative humidity sensor.  To measure the surrounding air this sensor uses a thermistor and a capacitive humidity sensor.For measuring temperature this sensor uses a Negative Temperature coefficient thermistor, which causes a decrease in its resistance value with increase in temperature.

The temperature range of DHT11 is from 0 to 50 degree Celsius with a 2-degree accuracy. Humidity range of this sensor is from 20 to 80% with 5% accuracy. The sampling rate of this sensor is 1Hz .i.e. it gives one reading for every second.  DHT11 is small in size with operating voltage from 3 to 5 volts. The maximum current used while measuring is 2.5mA.

DHT11 sensor has four pins- VCC, GND, Data Pin and a not connected pin. A pull-up resistor of 5k to 10k ohms is provided for communication between sensor and micro-controller.As amodule it has only 3 pins , pull up resistor provided on board.

Connect DATA pin of DHT11 to D2 of Arduino , Vcc to 5v & Gnd to Gnd.

Open NEXTION EDITOR and start a new project with .HMI file name.

Generate a FONT file and add to the project.

Click on Picture tab at bottom left corner and then + symbol to import a back ground picture.This picture resolution must match that of your NEXTION model

In this demo I’m using 4024T032 ( 400 X 240 pixel 3.2 inch size)

Use IRFANVIEW or INKSCAPE to match the resolution 400 x 240 of the image.

Click on PICTURE tool .It appears p0 on canvas.

At the right side window you can set the attributes for this object.Leave the object name as p0.

Click browse against pic attribute and select the image to be loaded as back ground.

Now the image appears on canvas.

Image 12

Add 2 TEXT objects  .One for Temperature and the other for Humidity.

As Nextion editor cannot handle fraction numbers , we use Text instead of Number object.

Following are the attribute settings for these 2 Text objects.

t0 objname can be changed to Temperature

vscope to global.All objects that is controlled through SERIAL to be set to global.

sta attribute by default is solid color.Change this to crop image.

Also select the browse against picc attribute and select the back ground image.

This creates a Transparent effect to the object.

Text color can be changed by the pco attribute. 65535 means White & 0 means Black.

You can click on Select against pco to select a color.

Same way repeat for the t1 object Humidity.

Image 13              Image 15

 

Next add on 4 more Text objects for display purpose.You need to change the txt attribute to change the display name .

txt – to change display name

pco – to change color of text.

sta -crop image  to give transparent effect so that background image is visible through this object.

Image 16

Click on DEBUG to start the Simulator.

You can test the display by sending STRING Commands.

temperature.txt=”32.0”

changes the Temperature display to 32.0.

Note the usage of double quotes for data to be displayed.While typing the code the interpreter of Nextion editor gives options to select (intellisense).

The commands can be sent as STRING from Arduino and to be followed by 3 bytes of 0xFF.

 

Image 17

Upload the following sketch on to Arduino.

Here we use the library for DHT Sensor by including the header file DHT.h

We define the DHT data pin connection as 2 (D2 of Arduino) and type as DHT11.

The variables temperature and humidity are initialized with float literal constants.

Reading of sensor is by calling functions readHumidity()  & readTemperature() from the Class dht.

The assigned data is sent to Nextion as a STRING command.

Image 1

 

Sending data.
Data to Nextion should be sent in the following format:

    • String command

=”humidity.txt=\””+String(humidity,1)+”\””;

  • name of the receiving element     humidity
  • sign “.”
  • field name of the receiving element    txt
  • sign “=”
  • data, and if it is String, then in quotation marks, and if it is digital information, then four bytes of data
  • three bytes 0xff
    Notice the use of ESCAPE character backslash \ before double quote “.
    This is how to send double quotes from Arduino Serially.

Nextion does not work with float. So all to be sent as – String

 

To upload the .tft file on to Nextion we use USB-TTL device.Connect Nextion to USB-TTL

Tx to Rx

Rx to Tx

Gnd – Gnd

5v to 5v

Click on UPLOAD button and select the COM port of USB device and a faster baudrate.

Once uploaded , remove USB device and connect Arduino with Nextion with same configuration as above.

The result is displayed on NEXTION DISPLAY.

The DHT sensor requires 1 second refresh rate for each reading.This is provided by 1 sec delay in Arduino code.

VIDEO :

 

blog_image

 

This post is on displaying potentiometer reading from Arduino to Nextion display.

 

IMG_20200413_180125

 

Open Nextion Editor and File –> New  to create a project file with extension .hmi

Always keep your project files in a separate folder.

In the next screen select your Nextion model display , mine is 4024T032 which is 400 x 240 pixel & 3.2 inch size Basic model.Also select the direction of display as Horizontal.

 

Image 1

Using Fonts Generator create a font file by selecting font size and font type.It is saved as .zi file.Click Yes to add font file to project.

The font file appears under font tab.

Image 3

Next to Fonts tab you see a Picture tab.

Click on that and then click on the + symbol seen on top f this window.

Browse to the location of the background image you want to import.

Remember the image must match the resolution of the display model.Here mine is 400 x 240 , so I’ve converted the image to 400 x 240 pixels using IRFANVIEW Software.

 

Image 4

Drag PICTURE tool from toolbox on to canvas or just click on Picture under tool box.

Picture component appears on canvas as p0 object name.

Click on that p0 , to modify attributes for object p0.

Against pic attribute click on browse and select the background picture from the list.

The back ground image appears on workspace canvas.

Image 6

 

Click on  SLIDER tool and then a NUMBER.

Slider appears as object h0   & number appears as object n0.

You may rename it from attribute section.But we shall leave it to default names.

 

Image 1

Following is the attribute window for the object h0.

The mode by default is Horizontal , if required can be changed to Vertical.

Color of the slider by default is Green , by clicking against bc0 attribute and select color , you can change it. pc0 is the color of slider knob, Red by default & can be changed.

Set the maxvalue to 1023 , as Analog read of Arduino ranges from 0-1023.

Image 2 - Copy

n0 Number attributes

bc0 , pc0 to change color of number.

vscope can be changed from local to global. If global it is available for all pages and if you control it from Arduino through Serial , make it GLOBAL.

 

Image 26

 

Click on DEBUG to start the Simulator.

Under simulator you can feed in the code to change value of n0 object.

n0.val=524 will change the n0 value to 524.

 

Image 3

Close the Simulator.

Now to upload the .tft file (after Compile you get this file)  on to Nextion , we use a USB-TTL device.

Connect Tx of Usb device to Rx of Nextion , Rx to Tx , Gnd to Gnd and Vcc to 5v.

 

IMG_20200414_155643

Plug in usb device to pc and note the COM port allotted to it.

Click on UPLOAD,

Select COM port and baud rate & click GO.

Image 21

To Arduino we have to upload the following sketch.

Image 6

 

analogRead of A0 pin where pot pin is connected is assigned to a integer variable value.

A string  n0.val is sent to serial along with this value .

The string is followed by 3 times 0xff hex value.This is the standard format Nextion will accept on its Serial.

Do not use any other Serial commands here.For e.g if you use Serial.println() , then Nextion will not display the value.

Once sketch is uploaded on to Arduino , connect Arduino to Nextion

Tx to Rx, Rx to Tx , 5v to 5v , Gnd to Gnd

Potentiometer Center pin to A0, one end of pot to +5v and other end to Gnd.

Now turning the potentiometer will vary the number on Display from 0 to 1023.

 

IMG_20200413_180125

Now let us modify the Arduino code so that Slider moves according to number value.

Serial.print(“click n0,1);

click n0,1 means  activate Press Event of object n0

upload this code on to Arduino.

Image 10

Now on Nextion Editor  click on n0 number object.

Under Touch Press Event feed in

h0.val=n0.val

The touch press event is activated by Arduin code , as if we were physically touching n0.

Now the event code is triggered where we assign value of slider to be same as value of number.

 

Image20

compile and upload the file to Nextion using USB-TTL.

Image34

Connect Nextion with Arduino nd see the result.

On varying the potentiometer the number value changes from 0 to 1023 .Accordingly the slider changes position.

Image 5

VIDEO :

 

 

 

blog_image

Nextion HMI Touch Displays are very popular in Industrial panel control boards.

This post is on usage of DUAL STATE BUTTON tool of the new NEXTION EDITOR to make an LED ON or OFF with switch images.

I’m using the model  NX 4024T032 display

which means  Resolution of 400 x 240 pixels & display size 3.2 inch

 

I2

 

Nextion Editor is used to create .tft file from .hmi file.

Download the latest version from :

https://nextion.tech/nextion-editor/

NO LIBRARY is used in this project.

N1

Open the Nextion Editor.

Always create a new folder for your project and save all files in that folder.

From Tools click on Fonts generator to generate font file for your project.

 

Image 4

Select the size of font and the type of font.

Provide a name and store this file.

The Nextion editor will store it as .zi file and add it to your project.

Image 5

At the bottom left corner you can see the Fonts tab under which your font file is added.

Image 6

Now click on the Picture tab to import some pictures for your project.

Remember to resize the background image to match the resolution of your Nextion display model.

Here mine is 400 x 240 pixels. I use IRFAN VIEW software to resize the image pixels to 400 x 240.

Other application like  INKSCAPE  is also powerful image tool like photoshop.

You can even use the Windows PAINT to edit.

Here are the images used in this project.

 

switch_off_1      switch_on2                img_ok

Click on the Picture tab and then the + symbol to import these pictures in to the Picture tab.

 

Image 3

From tool box double click on Picture tool.

A picture element appears on canvas with default name p0.

If you want you can rename the object name.

 

Image 7

At the right side attribute section there is an attribute called pic

Double click against the empty space near pic.

The images you imported appears .Select the back ground image and click ok.

Image 8

 

The background image appears on canvas if the resolution matches with that of your Nextion display model.

Image 9

Now from tool box double click on DOUBLE STSTE BUTTON.

An object with name bt0 appears on canvas.

 

Image 10

 

The main attribute of bt0 to change is the

sta attribute.   (status attribute)

Change the sta attribute from solid to image.

Image 11

Now you will get 2 more attributes pic0 & pic1

 

Image 12

Click against empty space of pic0 and select the OFF SWITCH image.

Click against empty space of pic0 and select ON SWITCH image.

Change the txt attribute to empty space.

Image 13

Now click on the DEBUG button on top.

Image 14

The SIMULATOR opens.

Without a physical NEXTION Display in hand , you can develop your project with NEXTION EDITOR.

 

Image 15

On Simulator you can test the ON / OFF condition which changes the image accordingly.

 

Image 16

Close the Simulator.

Click on bt0 dual state switch object.

Below the canvas you can see 2 Events.

Click on the TOUCH RELEASE EVENT.

Type in the following code :

print bt0.val

When the bt0 button is touched and on release this code is executed.

The value of bt0 button will be printed or sent out to serial as 4 bytes of HEX data..

 

Image 17

Click on DEBUG to test the code.

Image 18

On  simulator , if you touch and release the button once , the switch image changes and accordingly 4 bytes of HEX data sent

01 00 00 00  for ON

00 00 00 00 for OFF

Image 19

Close the simulator and add a TEXT object on the canvas.

You can change the attributes :

bc0 – back ground color

pc0 – text color

txt – name of text to display.

Image 21

Now under TOUCH RELEASE EVENT modify the code as :

 

print bt0.val
if(bt0.val==1)
{
t0.txt=”LED ON”
}else if(bt0.val==0)
{
t0.txt=”LED OFF”
}

 

We add an if condition to change the text value according to switch ON or OFF.uiuiui

 

Image 22

Click on DEBUG to see the result on simulator.

Image 24

 

Image 25

Once the testing is fine you can click on COMPILE to generate the .tft file.

Once the compilation is successful you can find the file under bianyi folder or tft output folder.

Image 26

To upload the .tft file on to Nextion display we use USB-TTL method.

Connect USB-TTL Nextion

Vcc—5v,  Gnd-Gnd , Tx—Rx ,, Rx –TX

 

I4

Plug in the USB-TTL to PC and note the COM port allotted under Device Manager.

Click on UPLOAD button.

Select the COM port and Baud rate as 115200.

Image 27

Now the file is uploaded and the screen we developed appears on Nextion screen.

Let us connect the Arduino with Nextion

Vcc—5v , Gnd – Gnd , Tx-Rx ,Rx-Tx

Upload the following code.While uploading remove the Tx,Rx wires and then connect back.

 

CODE

Once Done uploading you can test the project result.

In built LED on Arduino switched ON or OFF as per touch switch on the display.

 

Download code and images HERE :

 

I1

VIDEO :

 

blog_image

ULTRASONIC sensor is  popular in many applications where measuring distance or sensing objects are required.

The module has two eyes in the front which forms the Ultrasonic transmitter and Receiver.

U11

 

1.Vcc  The Vcc pin powers the sensor, typically with +5V,The current consumed by the sensor is less than 15mA

2.Trigger

Trigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending Ultrasonic burst of 40KHz.

3.Echo

Echo pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back from object to the sensor.

4.Ground

To start the measurement, the trigger pin has to be made high for 10uS and then turned off.

This action will trigger an ultrasonic wave at frequency of 40Hz from the transmitter and the receiver will wait for the wave to return.

Once the wave is returned after it getting reflected by any object the Echo pin goes high for a particular amount of time which will be equal to the time taken for the wave to return back to the sensor.

The amount of time during which the Echo pin stays high is measured by the esp32 as it gives the information about the time taken for the wave to return back to the Sensor. Using this information the distance is measured

As the ultrasonic sensor requires 5v , we use a separate power source.

This sensor will not operate on 3.3v.

Vcc –> 5v

Gnd to Gnd

Trigger –> GPIO 5

Echo  –>  GPIO 18

Both Gnd of 3.3v & that of 5v to be made common.

As the ECHO PIN of ultrasonic gives 5v level , we need a LEVEL SHIFTER in between ECHO & GPIO18

q1

 

IMG_20200328_133155

OLED connections :

Vcc to 3.3v

Gnd – Gnd

SCL –> GPIO 22

SDA –> GPIO 21

We use THONNY PYTHON to test and upload code.

2 library modules are required.

One ssd1306.py for OLED

hcsr04.py module for ultrasonic measurement support.

Download the files from :

http://www.alselectro.com/files/micropython_oled_dht_ultrasonic.zip

 

From ThonnyPython File –> open and browse to the location to select ssd1306.py

Save it to Micropython device as ssd1306.py

Image 14[5]

Same way import the hcsr04.py

and save it on Micropython device.

Image 12

Sample code is provided as main.py

After saving these file , the ESP32 has 4 files

boot.py

main.py

ssd1306.py

hcsr04.py

Image 15

Image 10

In the example code

we import classes Pin,I2C

Then we import modules ssd1306, hcsr04 and time

i2c is initiated with scl pin GPIO22 &  sda pin GPIO21

oled object is initiated by passing arguments width 128, height 64, protocol i2c and address 0x3c.

ultrasonic sensor object is instantiated from HCSR04 Class by passing on trigger pin GPIO5

Echo pin GPIO18

Inside never ending loop

distance_cm() method used to measure distance

oled.text() method to display result on OLED.

Note the conversion to STRING variable while dusplaying on OLED.

Image 11

 

Click on F5 to execute.

The OLED will display the sensor distance reading.

 

U1

VIDEO :

 

blog_image

DHT11 is a low-cost digital sensor for sensing temperature and humidity and can be easily interfaced with ESP32 on Micropython.

DHT11 sensor consists of a capacitive humidity sensing element and a thermistor for sensing temperature.

Temperature range of DHT11 is from 0 to 50 degree Celsius with a 2-degree accuracy.

Humidity range of this sensor is from 20 to 80% with 5% accuracy.

The sampling rate of this sensor is 1Hz .i.e. it gives one reading for every second.

DHT11 operating voltage from 3 to 5 volts. The maximum current consumed  is 2.5mA.

IMG_20200328_113814

DHT11 sensor has four pins- VCC, GND, Data Pin and a not connected pin. A pull-up resistor

of 5k to 10k ohms is needed for communication between sensor and micro-controller.

When you buy as Module , the resistor is provided on pcb , and only 3 pins available.

On module there is also a power LED provided.

Image 1

Connect GPIO 15  —> Data pin of DHT11

Gnd to Gnd , Vcc to 3.3v

OLED connections :

SCL –> GPIO 22

SDA –> GPIO 21

Gnd to Gnd , Vcc to 3.3v

 

Thonny Python is used as IDE to develop and upload code on to ESP32.

OLED driver module (library) to be downloaded from :

https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py

File –> open

browse to the location of ssd1306.py file to select it.

File –>  Save as

select Micropython device and save it as ssd1306.py file.

 

Image 6

From interpreter of Thonny Python code can be tested line by line.

Import from machine module Pin and I2C Class.

Import dht module , which is inbuilt in Micropython.

Image 11

Declare Pin 15 WHERE WE CONNECT data OF dht11 AS iNPUT.

Create  d , a dht object using class DHT11 ( from dht module) and pass on the argument pin 15.

DHT11 Class is in module dht.

so we use  dht.DHT11  instantiate an object from that class.

Image 2

measure() method is called to read the temperature and humidity values.

temperature()  method gives temp value

humidity() method gives % of humidity.

 

Image 3

 

Driver module for OLED and main.py files can be downloaded from :

http://www.alselectro.com/files/micropython_oled_dht_ultrasonic.zip

You can open the downloaded main.py file in Thonny python.

Click File –> Save as and select Micropython device

save as main.py file.

Do not change the name.It must be main.py , as Micropython after executing boot.py looks for main.py

In the example code

we import classes Pin,I2C

Then we import modules ssd1306, dht and time

i2c is initiated with scl pin GPIO22 &  sda pin GPIO21

oled object is initiated by passing arguments width 128, height 64, protocol i2c and address 0x3c.

d object is initiated using dht.DHT11 Class by passing on GPIO15

Inside never ending loop

measure()  method used to read temperature and humidity values.

temperature()

humidity()

methods used to get the result.

While printing on OLED we need to convert the data to STRING

oled.text( str ( t ),80,20)

 

Image 7

 

Image 4

Click on F5 to execute.

You can see the Temperature reading in Celcius & Humidity reading in % on OLED.

 

IMG_20200328_123149

Video :

 

blog_image

The mini OLED 0.96 in display is 128 x 64 pixels and communication protocol is I2C.

There is also a SPI version which has more pins 6 or 7.

The I2C model has only 4 pins Vcc,Gnd,SCL & SDA.

The supply volt is 3.3 to 5v. As an I2C device it has got an address , by default it is 0x3C or decimal 60. This can be changed by soldering the position of a resistor on the back of pcb.

 

ol3

Connect Vcc of OLED to 3.3v, Gnd to Gnd

SCL  –>  GPIO 22

SDA  –> GPIO 21

IMG_20200328_140033

Resolution of display is 128 x 64 pixels. 0 to 127 in X axis and 0 –63 on Y axis.

oled_pix

 

MicroPython provides some built-in support for these displays, and a simple framebuffer which can be used as a drawing surface.

However, we still need a driver to interface the buffer to the display.

There is a Python ssd1306 module for OLED displays available.This is python library for interfacing OLED (both I2C & SPI supported).

https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py

Click on Raw button and save as ssd1306.py file on your computer.This save as appears only in Chrome browser.

Open THONNY PYTHON

Use File  –> Open and browse to the location to select the ssd1306.py file.

Click File  –>  Save as

Select Micropython device (ESP32)

 

o1

Save the file as ssd1306.py

Now the driver module is on ESP32.

o2

By default already you have boot.py

Now we have loaded the driver ssd1306.py.

o3

From interpreter of Thonny Python , the individual code can be tested.

From machine module import Pin , I2C

Pin Class handles all GPIO pins and I2C handles i2c protocol..

Then import the OLED driver module ssd1306.

 

o4

To use the display you first need to create an I2C interface. In MicroPython I2C is  via a software implementation so you can select any GPIO pins you like.

We shall select the default SCL pin GPIO 22 &  SDA pin GPIO 21.

 

o5

 

Then create an oled object from class SSD1306_I2C

as this class is inside file ssd1306, we use

ssd1306.SSD1306_I2C

The arguments are  128 – width , 64 – height , i2c – protocol, 0x3c – address

 

o6

 

Once the object is instantiated , the methods can be used.

.text() method is to print message on display.

Pass on the message to print and the X,Y coordinates.

.show() method is used to show the message from buffer to display.

Unless you issue this oled.show() , you cannot see the result.

 

o7

 

The framebuffer class provides support for writing text using a simple 8×8 bitmap font. The pixel x,y positions are relative to the top-left of the 8×8 character, so positioning at 0,0 will give the absolute top left of the screen.

Here we have selected x= 30, y =30 POSITION.

 

IMG_20200330_055934

 

To CLEAR the display we call   fill ( 0) method an then show()

 

o8

 

To draw an illuminated  line. Origin  Origin (10,10) and final (80,20)

For illuminated , we use 1 at end

o9

 

IMG_20200330_060216

 

Draw an illuminated horizontal line. Origin (5, 30) width 100 pixels

o10

 

IMG_20200330_060410

Draw an illuminated filled rectangle. Origin (20,10) and width x height 60×40 pixels

o11

 

IMG_20200330_060526

 

 

The ssd1306 object itself  provides methods for direct control of the display component.

The display can be turned on and off using

oled.poweron() and oled.poweroff() respectively.

o13

 

You can set the contrast for the display using

.contrast() passing in parameter c which is a value between 0 and 255. This controls the contrast between the foreground, active colour 1 and the background 0.

o14

To invert the display, switching foreground and background colours, call .invert(1).

Calling .invert(0) will return the display to how it was originally. You can use this for display-flashing visual effects:

o15

Download code and library module from here :

http://www.alselectro.com/files/micropython_oled_dht_ultrasonic.zip

After testing OLED using interpreter , we shall write code on to EDITOR of Thonny Python.

ol1

Save this file as main.py on Micro Python device.

Now you have 3 files on ESP32

boot.py

main.py

ssd1306.py

ol2

 

Micropython executes boot.py on power on , then looks for main.py to execute.

Click on F5 to execute and see the result on OLED display.

Video :

 

 

blog_image

 

ADC3

ldr2_adc

Built in ADC of ESP32 can be used to read analog values from sensors like potentiometer,LDR,Load cell,Thermistor,etc..

On the ESP32 ADC functionality is available on Pins 32-39.

Only the pins of ADC1 are enabled for ADC.  ADC2 is not enabled in Micropython.

pins 36-39 are recommended for analog input due to the available preamplifier.

ESP32 ADC pins by default have 12 bit resolution.

These pins read voltage between 0 to 3.3v & return value between 0 and 4095

To get value bet 0-1023 CHANGE RESOLUTION TO 10 BIT

IMG_20200326_123034

Connect a LDR to+3.3v , other end to Gnd through 10k Resistor.

The junction point of LDR & Resistor is connected to ADC GPIO Pin 36.

Open Thonny Python & use the interpreter to test ADC code.

From machine module import classes Pin & ADC.

Create ad adc object by passing on the GPIO Pin 36.

Read the analog value using read() method.

LDR faced to light gives some resistance , and the value is 1348.

 

ad1

This function returns a whole number between 0 and 4095. Because we know that 0 refers

to 0 V and 4095 refers to 3.3 V

In full dark condition (LDR closed) the value is 4095.

The ADC resolution by default is 12 bits

2 power 12 is 4096.

ad2

We can convert the reading to voltage by multiplying with 0.000805

2 power 12 is 4096

Voltage is 3.3v ,   3.3/4096 = 0.000805

You can see , at full dark condition the ADC value is 3.3v

ad3

As per documentation of ESP32 the maximum ADC volt can be set using attenuation.

ADC.atten(attenuation)

This method allows for the setting of the amount of attenuation on the input of the ADC.

This allows for a wider possible input voltage range, at the cost of accuracy

(the same number of bits now represents a wider range). The possible attenuation options are:

ADC.ATTN_0DB: 0dB attenuation, gives a maximum input voltage of 1.00v – this is the default configuration  (but while testing it shows 3.3v)

ADC.ATTN_2_5DB: 2.5dB attenuation, gives a maximum input voltage of approximately 1.34v

ADC.ATTN_6DB: 6dB attenuation, gives a maximum input voltage of approximately 2.00v

ADC.ATTN_11DB: 11dB attenuation, gives a maximum input voltage of approximately 3.6v

 

Setting the attenuation to 11DB gives value of 1.72 volt ( but the document says 3.3v)

So leave it to default 0DB which practically gives 3.3v

ad4

The resolution can be changed to 10 bit by setting width to 10BIT.

Default is 12 BIT.

 

ADC.width(width)

This method allows for the setting of the number of bits to be utilised and returned during ADC reads. Possible width options are:

ADC.WIDTH_9BIT: 9 bit data

ADC.WIDTH_10BIT: 10 bit data

ADC.WIDTH_11BIT: 11 bit data

ADC.WIDTH_12BIT: 12 bit data – this is the default configuration

ad5

 

ad6

After testing with interpreter we shall use the Editor to write code.

Save the file as main.py on to Micropython device.

Click on F5 to execute.

The analog output value is displayed according to light fall on LDR.

adc12

Video :

 

blog_image