element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
element14's The Ben Heck Show
  • Challenges & Projects
  • element14 presents
  • element14's The Ben Heck Show
  • More
  • Cancel
element14's The Ben Heck Show
Forum Raspberry Pi: Analog audio affecting status of LEDs in Python
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join element14's The Ben Heck Show to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 1 reply
  • Subscribers 29 subscribers
  • Views 316 views
  • Users 0 members are here
Related

Raspberry Pi: Analog audio affecting status of LEDs in Python

Former Member
Former Member over 11 years ago

I am fairly confident with changing the status of LEDs connected to the GPIO via Python in the Raspberty Pi however I am interested in manipulating the status of the LEDs according to how loud the analog audio is that is being pumped into the GPIO. I have a few questions though...

 

  • Is it possible for me to connect the analog audio from an iPod for example into the GPIO on the Raspberry Pi?
  • How can I detect the amplitude of the audio being received?
  • How would I make an LED turn on when the audio had exceeded a certain amplitude?

 

Thanks

  • Sign in to reply
  • Cancel
  • danielmcgraw
    danielmcgraw over 11 years ago

    Hi,

    It is not possible to directly measure an analog input into the Raspberry Pi. This has to be done using an external ADC (analog to digital converter) chip. These measure an analog voltage and convert it to a digital value linearly. Important points with ADC:

    • RESOLUTION: measured in bits, this is how accurately the ADC can measure the value. It will split between the supply voltage and 0V into 2 to the power of n steps where n is the number of bits. So a 10 bit ADC powered off 5V will split 0V-5V into 1024 equal steps (2^10) so an input of 2.5V will produce a digital output of 512. Assuming you are making a VU meter, make sure you have enough bits to provide enough combinations for you LEDs. That said, a small 8 bit ADC still has 255 lit combinations ( 1 of the combinations is 0) so you could have 255 LEDs and I doubt you'll be using more than that.
    • SAMPLE RATE: measured in samples per second, this is how often the ADC will measure the input voltage. If your using it for audio, it doesn't need to be sampled very often so any ADC with a sample rate > 20SPS (samples per second) will do. Most range in the 1000s of samples per second so any device should be fine.
    • INTERFACE: this is how the ADC will tell the Raspberry Pi what the digital value is. Common examples are serial, I2C and SPI. For the Raspberry Pi, I would recommend serial or I2C, there are many tutorials on how to set both of these up. http://www.instructables.com/id/Raspberry-Pi-I2C-Python/?ALLSTEPS for I2C or http://codeandlife.com/2012/12/27/raspberry-pi-as-arduino-hdmi-shield/ for serial


    Assuming you use I2C (which is what I recommend), once you have set the RPi up as in the I2C instructions above, the code would be:

     

     

    import smbus

    import RPi.GPIO as GPIO

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(10, GPIO.OUT)

    bus = smbus.SMBus(1)

    address = 0x60

    def read():
            signal = bus.read_byte_data(address, 1)

            if signal>512:

                   GPIO.output(10,True)

            else:

                   GPIO.output(10,False)
           
    Going through 1 line at a time

    import smbus : import the python i2c library

    import RPi.GPIO as GPIO: import the GPIO library and define it as "GPIO"

    GPIO.setmode(GPIO.BOARD): set the GPIO names to their physical pin number (so GPIO 10 is pin 10 on the GPIO header)

    GPIO.setup(10, GPIO.OUT): set pin 10 as an output

    bus = smbus.SMBus(1): give the name "bus" to the i2c bus. On newer Raspberry Pi, the bus is number 1, on older RPi's, its number 0. To find out go into terminal, with the ADC connected and powered and type in "i2cdetect -y 0" and "i2cdetect -y 1" This will tell you what is connected to each I2C bus, use the one that shows it has something connected to it (see I2C tutorial)

    address = 0x60: set the address of the ADC chip to 0x60 (found when running i2cdetect - see tutorial)

    def read(): start a procedure called read
    signal = bus.read_byte_data(address, 1): read register 1 on the ADC chip and store it as the variable "signal"

    if signal>512: if the signal is greater than 512, in a 10 bit ADC this would be half of supply voltage e.g. 2.5V

    GPIO.output(10,True): set pin 10 to logic high (turning LED on)

    else:  otherwise

    GPIO.output(10,False): set pin 10 to logic low (LED off)

    This means everytime the read procedure is called, the Raspberry Pi will measure the voltage, if it's greater than half the supply voltage, turn the LED on, else turn the LED off. Combine if statements to turn lots of LED's on or off in different combinations depending on the signal level.

    Any further questions, just ask

    Good luck

    Dan McGraw

    M0WUT

    • Cancel
    • Vote Up 0 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 © 2026 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