Editing Tutorial Analog IN

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
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 30: Line 14:
 
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 and Code Example =
  
== Schematics for connecting to ESP32 ==
+
== Schematics ==
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)
 
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]]
 
[[File:R2.JPG|300px]]
  
 
Connecting the potentiometer (poti) or slider. The value you read is dependent on where your poti or slider stands.
 
Connecting the potentiometer (poti) or slider. The value you read is dependent on where your poti or slider stands.
 
 
[[File:Rpoti.JPG|300px]]
 
[[File:Rpoti.JPG|300px]]
  
 
Connecting the light dependent resistor (LDR). The value you read is dependent on how bright it is.
 
Connecting the light dependent resistor (LDR). The value you read is dependent on how bright it is.
 +
[[File:Rldr.JPG|300px]]
  
[[File:Rldr.JPG|300px]]
 
  
== Code Example ESP32 ==
+
== Code Example ==
 
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-
 
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'>
 
<syntaxhighlight lang="python" line='line'>
#Example usage for ESP32
+
#Example usage
 
from machine import Pin, ADC
 
from machine import Pin, ADC
 
from time import sleep
 
from time import sleep
Line 64: Line 40:
 
analogPin = ADC(Pin(34))
 
analogPin = ADC(Pin(34))
 
analogPin.atten(ADC.ATTN_11DB)
 
analogPin.atten(ADC.ATTN_11DB)
 
while True:
 
  analogVal = analogPin.read()
 
  print(analogVal)
 
  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:
 
while True:

Please note that all contributions to Sketching with Hardware at LMU Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see My wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)