In this article, we’re exploring the fascinating interplay between sensors, Analog-Digital Converters (ADCs), and MicroPython to create intuitive and responsive projects.
Translating the Real World into Machine LanguageSensors play a pivotal role in bridging the gap between the physical world and digital realm, by translating real-world parameters into signals that machines can decipher. Predominantly, this translation can occur through varying voltage levels which act as a medium for conveying different sets of information.
The Functionality of ADCs in MicrocontrollersMicrocontrollers are equipped with a significant component known as the Analog-Digital Converter, or simply, ADC. This unit is responsible for interpreting the varying voltage levels generated by sensors and converting them into a digital format that can be processed further, acting as a conduit between sensors and microcontrollers.
Harnessing ADCs with MicroPython on the PSoC6Before diving into working with the ADC, we highly recommend reviewing the "Micropython on PSoC6" Protip. This comprehensive guide will walk you through installing Micropython on the PSoC6 and demonstrate how to operate its onboard LED.
Step 0: Hardware SetupEnsure your ADC sensor or input is correctly connected to the PSoC6’s ADC pin.
You should know that the PSoC6 supports only one 12-bit SAR ADC with the following channel-to-pin mapping and the defaults are set accordingly:
- Channel 0: Pin P10_0
- Channel 1: Pin P10_1
- Channel 2: Pin P10_2
- Channel 3: Pin P10_3
- Channel 4: Pin P10_4
- Channel 5: Pin P10_5
A Successive Approximation Register (SAR) ADC is a type of analog-to-digital converter that works by comparing the input analog voltage to a series of reference voltages generated through a binary search algorithm. This process happens iteratively, starting from the most significant bit to the least significant bit, to find the digital representation of the input voltage. It offers a good balance of speed, accuracy, and power consumption, making it suitable for various applications.Step 1: Import Necessary Libraries
Ensure that you have the necessary libraries imported into your script:
from machine import ADC, Pin
The machine
module is a core component of MicroPython that provides functionalities to interact directly with the hardware components of a microcontroller. By leveraging the functionalities offered by this module, developers can gain direct control over the I/O pins, pulse width modulation (PWM), I2C, SPI, UART communication peripherals, and more.
Step 2: Create an ADC ObjectInitialize an ADC object to represent the ADC hardware block on your microcontroller.
adc = ADC(Pin("P10_0")) # create an ADC object on ADC pinStep 3: Connect a Channel to a Specific Pin
Step 3: Read the Analog VoltageRead the analog voltage at the connected pin using the read_u16() method, which returns the voltage value in in range of 0 to 65535.
val = adc.read_u16() # read a raw analog value in the range 0-65535
Now what you have to do is get the fraction required from the range by dividing by 65535 and then multiplying with 3.3V.
val = adc.read_u16() / 65535 # read a raw analog value in the range 0-65535
analogValue=3.3*val
print(f"The analog value obtained from connecting to a 3,3V supply is {analogValue}")
Let's make an example where we keep reading an analog value every 0.5 seconds
from time import sleep
from machine import ADC, Pin
adc = ADC(Pin("P10_0")) # create an ADC object on ADC pin
while True:
val = adc.read_u16() / 65535
analogValue=3.3*val
print(f"The analog value obtained from connecting to a 3,3V supply is {analogValue}")
sleep(0.5)
You've now mastered the basics of interfacing sensors with ADCs on the PSoC6 platform using MicroPython. With the knowledge gained from this guide, you can embark on creating more advanced projects. Remember to refer back to the guidance in this Protip and the "Micropython on PSoC6" and "Breaking the Programming Barrier: CO2 + PSoC and MicroPython" Protips for technical support. Happy coding, and here's to bringing many more innovative projects to life!
Comments