element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Raspberry Pi Pico MicroPython creating u16 values for duty cycle
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 5 replies
  • Subscribers 661 subscribers
  • Views 5987 views
  • Users 0 members are here
  • python software
  • raspberry pi pico
  • raspberry_pi_pico
Related

Raspberry Pi Pico MicroPython creating u16 values for duty cycle

robogary
robogary over 3 years ago

I'm teaching a hands on Raspberry Pi Pico class at my local library using Thonny Python.

The class is mostly doing small hands on projects, explain the wiring, explain the code, expound on any principles that are part of the lesson, let the students type in the code and watch it run.

 

The next class topic is reading analog inputs and doing something physical with it. 

  • In code pasted below  ReadPot_GP26_DimLED_GP2.py The analog input's u16 value is read and directly used for writing a PWM u16 duty cycle value, and works fine.
  • I got really lucky with servo PWM code, the  duty cycles are so small, 5% to 10%, the python interpreter doesnt issue with the duty values.
  • Creating some control examples using a photoresistor, those programs work well, until I try to write a value as a u16. See program DuskToDawnLEDwDimmer.py.

       The DuskToDawnLEDwDimmer.py. program has a parameter to turn on a LED at a certain level of light on the photoresistor.  At that turn on point, the task is to have an LED at its full brightess (PWM duty = 100%) and the LED is to dim itself as the ambient light lessens (gets dark) . I tried everything I could think of to get the LED's dimmer duty cycle = 65535 and have it infinitely adjustable down to 0.  I could not create any variable that was u16 and satisfy Python compilation. My brute force solution was to check for photoresistor level intensity points based on volts, and write a constant value to duty cycle.

 

Has anyone else had issues writing u16 values ?  Any suggestions on approaching the coding differently ?

 

 

 

ReadPot_GP26_DimLED_GP2.py

This is an example that works w

from machine import Pin, ADC, PWM

import utime

PWM1A=PWM(Pin(2)) # GP2  pin 4

PWM1A.freq(1000)

 

PotWiperCounts=ADC(Pin(26)) # can also call machine.ADC(3)

#GP2OFFSET=288  # The PotWiperCounts when the pot is full CCW

#GP2GAIN=1.004 # GP2 read 65535 at full cw, however once 288 counts are

# subtracted from it, it needs multiplied by the ratio to make it back to 65535 full scale

 

 

while True:

    print(PotWiperCounts.read_u16())

#    PotWiperCountsReal=PotWiperCounts*1.0

#    PotWiperCountsReal=(PotWiperCountsReal-(GP2OFFSET))

# yes, in code you can read a value, do math, and put the value back to the same name

#    PotWiperCountsReal=1.004*PotWiperCounts

    duty=PotWiperCounts.read_u16()

    PWM1A.duty_u16(duty)

    utime.sleep(.05)

#    print("Adjusted GP2 value is:", PotWiperCountsReal)

   

# MAKE a NOTE what the PotWiperCounts are at full CW - full up________

# MAKE a NOTE what the PotWiperCounts are at full CCW - full down________

# The value at full CCW should be zero. The value it really shows is called OFFSET !!

# The next program you will build will have parameters to correct the OFFSET and then also adjust the gain error - The value at full CW !

 

 

DuskToDawnLEDwDimmer.py

from machine import Pin, ADC, PWM

import utime

#import constant  # wierd

 

 

PotWiperCounts=ADC(Pin(26))

LDR_Counts=ADC(Pin(27))

 

u16_to_volts = 3.3/(65535)

#volts_2_U16 = (65535)/3.3

 

# DuskToDawnLED= Pin(25, machine.Pin.OUT)#GP25 is the Pico mounted LED

# DuskToDawnLED.value(0)

DuskToDawnLED= PWM(Pin(2))# physical pin 4   

DuskToDawnLED.freq(100)

 

LightOnAdjust=1.5 ## in volts at GPIO 

# volts_to_U16 = (65535)/LightOnAdjust

 

volts_to_U16 = (65535)/LightOnAdjust

 

while True:

    #print(PotWiperCounts.read_u16())

    #print(LDR_Counts.read_u16())

    LDR_Volts=(LDR_Counts.read_u16())*u16_to_volts

    print(LDR_Volts)

    if ((LDR_Volts) < (LightOnAdjust)):   ## floating point value of volts

        DimmerDutyCounts=65535

    if ((LDR_Volts) < ((LightOnAdjust)*.8)):   ## floating point value of volts

        DimmerDutyCounts=52400

    if ((LDR_Volts) < ((LightOnAdjust)*.6)):   ## floating point value of volts

        DimmerDutyCounts=39000

    if ((LDR_Volts) < ((LightOnAdjust)*.4)):   ## floating point value of volts

        DimmerDutyCounts=26200

    if ((LDR_Volts) < ((LightOnAdjust)*.2)):   ## floating point value of volts

        DimmerDutyCounts=13000

    if ((LDR_Volts) > ((LightOnAdjust))):   ## floating point value of volts

        DimmerDutyCounts=0       

    DuskToDawnLED.duty_u16(DimmerDutyCounts)

           

    utime.sleep(2)

  • Sign in to reply
  • Cancel
  • shabaz
    shabaz over 3 years ago

    Hi Gary,

     

    I didn't entirely understand the problem, but if it you require 16-bit unsigned int, I think (not sure if there's another way or not) you could either just try to force integers by typing (say) int(value) although it won't be 16 bit internally, it will take only integer values. Alternatively there is bytes or bytearray, which will allow you to have two bytes to represent the 16-bit number. In any case, even for type interger, you can read and work with individual bits, which is perhaps what the library may be doing to convert the value into a stream of data bits for (say) a serial interface.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 3 years ago

    I suspect you are likely just missing the conversion to int:

    image

     

    I don't know why they don't support floats as inputs, the whole point of using python is to make it easy!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • robogary
    robogary over 3 years ago in reply to scottiebabe

    Thanks for the ideas,I'll have to give them a shot.

    Whats wierd, the line of code    PWM1A.duty_u16(duty)   works great because it is preceded with: duty=PotWiperCounts.read_u16()

    I have another program writing PWM to servos, where the duty value never exceeds 6553 , and that works fine..........

     

    expression DuskToDawnLED.duty_u16(Duty)  would bail out if the duty value exceeds 32767 , after setting the 16th bit.

     

    if Duty is just assigned a constant it works OK, and the code never claimed it was an INT.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • robogary
    robogary over 3 years ago in reply to shabaz

    ROFL - thanks, what I discovered was just exactly what you replied !  

    Python didnt care about distinctly defining a u16 word. It just took the float value and converted to INT. 

    The PWM duty command says its needs a u16, but if the value is lower than 32767, it really doesnt care what it is.

    As long as the INT value is below 65535 all is well .  Sometimes I overthink these things and force it too much like C coding.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 3 years ago in reply to scottiebabe

    Converting from float to int with the int() function may not be a good habit to have, as I recently discovered with spurs in my DDS lookup table.

     

    image

    The int() converts from floating point to integer via truncation and round converts with its own set of rounding rules.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube