Difference between revisions of "Tutorial Analog IN"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
(Created page with "= Analog Input with MicroPyton and ESP32/ESP8266 = In this part of the tutorial, we demonstrate how to read in an analog value using MicroPyton on ESP32/ESP8266. We also show...")
 
 
(14 intermediate revisions by 2 users not shown)
Line 6: Line 6:
 
* you understand how to build a voltage divider to read a sensor value from a changing resistor
 
* you understand how to build a voltage divider to read a sensor value from a changing resistor
 
* you can connect a Potentiometer, a slider, and a [[LDR]] to the ESP32/ESP8266 and read the value
 
* you can connect a Potentiometer, a slider, and a [[LDR]] to the ESP32/ESP8266 and read the value
 +
 +
 +
== Related Components ==
 +
The components are related to the [[LMUBox]]. For more components, see the [[Hardware List]. Many of the pages on actuators and sensors include additional examples.
 +
 +
=== Microcontroller ===
 +
* [[ESP32 Web Kit]] with integrated OLED Display from Heltec
 +
* [[ESP8266 ESP-12F]] NodeMCU Module from AZDelivery
 +
 +
=== Sensors (and physical controllers) ===
 +
* [[LDR]] Light dependend resistor
 +
* [[Potentiometer]] short Poti, changeable resistors as slider and knobs
 +
* [[Analog Joystick]]
  
 
= Instructional Videos =
 
= Instructional Videos =
 
The videos are a first test... the ones marked with ** will be replaced over the next months with new versions.
 
The videos are a first test... the ones marked with ** will be replaced over the next months with new versions.
 +
 +
If you use the [[ESP8266_D1_Mini]] have a look at this file about which Pins to use: https://micropython-on-wemos-d1-mini.readthedocs.io/en/latest/basics.html
 +
  
  
Line 14: Line 30:
 
In this video on youtube (46:23) we show how to connect to read in an analog value with MicroPython. We also show how a sensor that is based on a changing resistance can be connected using a voltage divider: https://youtu.be/gjj5KyK2qGI   
 
In this video on youtube (46:23) we show how to connect to read in an analog value with MicroPython. We also show how a sensor that is based on a changing resistance can be connected using a voltage divider: https://youtu.be/gjj5KyK2qGI   
  
<youtube>gjj5KyK2qGI </youtube>
+
<youtube>gjj5KyK2qGI</youtube>
 +
 
 +
 
 +
Here is a short video of how to connect a potentiometer and how to read an analog value on the ESP8266_D1_Mini.
 +
 
 +
<youtube>gToR5wwcxuQ</youtube>
 +
 
 +
= Schematics and Code Example =
 +
 
 +
== Schematics for connecting to ESP32 ==
 +
For the ESP8266 use A0 instead of Pin 34.
 +
 
 +
This should divide the voltage by half as both resistors are the same - you should read a value of about 2000 (assuming 12Bit resolution with a range of 0-4096)
 +
 
 +
[[File:R2.JPG|300px]]
 +
 
 +
Connecting the potentiometer (poti) or slider. The value you read is dependent on where your poti or slider stands.
 +
 
 +
[[File:Rpoti.JPG|300px]]
 +
 
 +
Connecting the light dependent resistor (LDR). The value you read is dependent on how bright it is.
 +
 
 +
[[File:Rldr.JPG|300px]]
 +
 
 +
== Code Example ESP32 ==
 +
This makes Pin 34 an analog input and set it to 12 bit. It reads the analog value every second and print it to the console-
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
#Example usage for ESP32
 +
from machine import Pin, ADC
 +
from time import sleep
 +
 
 +
analogPin = ADC(Pin(34))
 +
analogPin.atten(ADC.ATTN_11DB)
 +
 
 +
while True:
 +
  analogVal = analogPin.read()
 +
  print(analogVal)
 +
  sleep(1)
 +
</syntaxhighlight>
 +
 
 +
== Code Example Arduino Nano Connect RP2040 ==
 +
A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
#Example usage for Arduino Nano
 +
from machine import Pin, ADC
 +
from time import sleep
 +
 
 +
analogPin = ADC(Pin(26))
 +
 
 +
while True:
 +
  analogVal16 = analogPin.read_u16()
 +
  print(analogVal16)
 +
  sleep(1)
 +
</syntaxhighlight>
 +
 
 +
== Code Example ESP8266 ==
 +
A0 is the analog input with 10 bit resolution. It reads the analog value every second and print it to the console-
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
#Example usage for ESP8266
 +
from machine import Pin, ADC
 +
from time import sleep
 +
 
 +
analogPin = ADC(0)
 +
 
 +
while True:
 +
  analogVal = analogPin.read()
 +
  print(analogVal)
 +
  sleep(1)
 +
</syntaxhighlight>
 +
 
 +
[[Category:Tutorials]]

Latest revision as of 15:13, 12 June 2024

Analog Input with MicroPyton and ESP32/ESP8266[edit]

In this part of the tutorial, we demonstrate how to read in an analog value using MicroPyton on ESP32/ESP8266. We also show how we can use the analog input to measure a changing resistor. This includes looking at the voltage range that can be measured the resolution of the analog-digital converter (ADC).

Success criteria[edit]

  • you can read in an analog value from the python prompt
  • you understand how to build a voltage divider to read a sensor value from a changing resistor
  • you can connect a Potentiometer, a slider, and a LDR to the ESP32/ESP8266 and read the value


Related Components[edit]

The components are related to the LMUBox. For more components, see the [[Hardware List]. Many of the pages on actuators and sensors include additional examples.

Microcontroller[edit]

Sensors (and physical controllers)[edit]

Instructional Videos[edit]

The videos are a first test... the ones marked with ** will be replaced over the next months with new versions.

If you use the ESP8266_D1_Mini have a look at this file about which Pins to use: https://micropython-on-wemos-d1-mini.readthedocs.io/en/latest/basics.html


Analog Input - ADC - connecting a variable resistor **[edit]

In this video on youtube (46:23) we show how to connect to read in an analog value with MicroPython. We also show how a sensor that is based on a changing resistance can be connected using a voltage divider: https://youtu.be/gjj5KyK2qGI


Here is a short video of how to connect a potentiometer and how to read an analog value on the ESP8266_D1_Mini.

Schematics and Code Example[edit]

Schematics for connecting to ESP32[edit]

For the ESP8266 use A0 instead of Pin 34.

This should divide the voltage by half as both resistors are the same - you should read a value of about 2000 (assuming 12Bit resolution with a range of 0-4096)

R2.JPG

Connecting the potentiometer (poti) or slider. The value you read is dependent on where your poti or slider stands.

Rpoti.JPG

Connecting the light dependent resistor (LDR). The value you read is dependent on how bright it is.

Rldr.JPG

Code Example ESP32[edit]

This makes Pin 34 an analog input and set it to 12 bit. It reads the analog value every second and print it to the console-

 1 #Example usage for ESP32
 2 from machine import Pin, ADC
 3 from time import sleep
 4 
 5 analogPin = ADC(Pin(34))
 6 analogPin.atten(ADC.ATTN_11DB)
 7 
 8 while True:
 9   analogVal = analogPin.read()
10   print(analogVal)
11   sleep(1)

Code Example Arduino Nano Connect RP2040[edit]

A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-

 1 #Example usage for Arduino Nano
 2 from machine import Pin, ADC
 3 from time import sleep
 4 
 5 analogPin = ADC(Pin(26))
 6 
 7 while True:
 8   analogVal16 = analogPin.read_u16()
 9   print(analogVal16)
10   sleep(1)

Code Example ESP8266[edit]

A0 is the analog input with 10 bit resolution. It reads the analog value every second and print it to the console-

 1 #Example usage for ESP8266
 2 from machine import Pin, ADC
 3 from time import sleep
 4 
 5 analogPin = ADC(0)
 6 
 7 while True:
 8   analogVal = analogPin.read()
 9   print(analogVal)
10   sleep(1)