Before you run the code, you'll need to set ADAFRUIT_IO_KEY
and ADAFRUIT_IO_USERNAME
to the key and username associated with your Adafruit IO account.
# Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'YOUR_USERNAME' # Create an instance of the REST client. aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
The analog_output example uses the PCA9685's channel 15 by default. If you wish to change it, you can modify the PCA_CHANNEL
variable at the top of the code to correspond with a different PWM channel of the PCA9685.
# Set the PCA channel to use. PCA_CHANNEL = 15
Inside the while True
loop, we grab the analog feed value and compare it to the previous analog feed value.
We wan't the LED's brightness to remain set to the previous feed value, unless changed.
To do this, we first check it's a new value (the values are not equal). Then, we print that we've received a new value, map the feed range to a write-able value range, and set the PCA's channel to this value. If we don't receive a new value, we simply store the previous value in the prev_read
variable.
while True: # grab the `analog` feed value analog_read = aio.receive(analog.key) if (analog_read.value != prev_read): print('received <- ', analog_read.value) # map the analog value from 0 - 1023 to 0 - 65534 analog_value = map_range(int(analog_read.value), 0, 1024, 0, 65534) # set the LED to the mapped feed value pca.channels[PCA_CHANNEL].duty_cycle = int(analog_value) prev_read = analog_read.value
Run the code by entering the following in your terminal:
python3 analog_output.py
Change the value of the slider on your Adafruit IO dashboard, and you should see something resembling the following in the terminal:
$ python3 analog-out.py received <- 300 received <- 500 received <- 100
You should also see your LED change brightness depending on the value you send.
""" `analog_output.py` =============================================================================== PWM a LED from Adafruit IO! Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-analog-output Adafruit invests time and resources providing this open source code. Please support Adafruit and open source hardware by purchasing products from Adafruit! Author(s): Brent Rubell for Adafruit Industries Copyright (c) 2018 Adafruit Industries Licensed under the MIT license. All text above must be included in any redistribution. Dependencies: - Adafruit_Blinka (https://github.com/adafruit/Adafruit_Blinka) - Adafruit_CircuitPython_PCA9685 (https://github.com/adafruit/Adafruit_CircuitPython_PCA9685) """ # import system libraries import time # import Adafruit Blinka from board import SCL, SDA from busio import I2C # import the PCA9685 module. from adafruit_pca9685 import PCA9685 # import Adafruit IO REST client from Adafruit_IO import Client, Feed, RequestError # Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'YOUR_IO_KEY' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'YOUR_IO_USERNAME' # Create the I2C bus interface. i2c_bus = I2C(SCL, SDA) # Create a simple PCA9685 class instance. pca = PCA9685(i2c_bus) PCA_CHANNEL = 4 # Set the PWM frequency to 60hz. pca.frequency = 60 prev_read = 0 # Create an instance of the REST client. aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) try: # if we have a 'analog' feed analog = aio.feeds('analog') except RequestError: # create an `analog` feed feed = Feed(name='analog') analog = aio.create_feed(feed) def map_range(x, in_min, in_max, out_min, out_max): """re-maps a number from one range to another.""" mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min if out_min <= out_max: return max(min(mapped, out_max), out_min) return min(max(mapped, out_max), out_min) while True: # grab the `analog` feed value analog_read = aio.receive(analog.key) if analog_read.value != prev_read: print('received <- ', analog_read.value) # map the analog value from 0 - 1023 to 0 - 65534 analog_value = map_range(int(analog_read.value), 0, 1024, 0, 65534) # set the LED to the mapped feed value pca.channels[PCA_CHANNEL].duty_cycle = int(analog_value) prev_read = analog_read.value # timeout so we don't flood IO with requests time.sleep(0.5)
Text editor powered by tinymce.