PWM Guide: Zen Buzzer and Tri-Colour LEDs For Linux Kernel 4.1+ Table of Contents. by Brian Fraser Last update: November 17, 2017

Similar documents
Adafruit 16 Channel Servo Driver with Raspberry Pi

Fading a RGB LED on BeagleBone Black

Outernet L-band for Linux Documentation

Adafruit 16-Channel PWM/Servo HAT & Bonnet for Raspberry Pi

Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi

ZIO Python API. Tutorial. 1.1, May 2009

Running the Processing environment on ARM SBCs

For this exercise, you will need a partner, an Arduino kit (in the plastic tub), and a laptop with the Arduino programming environment.

Course Introduction. Content 20 pages 3 questions. Learning Time 30 minutes

DI-1100 USB Data Acquisition (DAQ) System Communication Protocol

I2C Demonstration Board LED Dimmers and Blinkers PCA9531 and PCA9551

CodeBug I2C Tether Documentation

Rochester Institute of Technology Real Time and Embedded Systems: Project 2a

Motor Driver HAT User Manual

PWM LED Color Control

MD04-24Volt 20Amp H Bridge Motor Drive

MBI5051/MBI5052/MBI5053 Application Note

Installing and Configuring SIV

EE 314 Spring 2003 Microprocessor Systems

The Information contained herein is subject to change without notice. Revisions may be issued regarding changes and/or additions.

Page 1/10 Digilent Analog Discovery (DAD) Tutorial 6-Aug-15. Figure 2: DAD pin configuration

RB-Dev-03 Devantech CMPS03 Magnetic Compass Module

Adafruit PCA9685 Library Documentation

Exercise 3: Sound volume robot

Temperature Monitoring and Fan Control with Platform Manager 2

Adafruit 16-Channel Servo Driver with Arduino

Adafruit 16-channel PWM/Servo Shield

Adafruit 16-Channel Servo Driver with Arduino

EE 308 Lab Spring 2009

Class #25: Digital Electronics and Software Python2.7-Based Control and Data Acquisition

High Resolution Pulse Generation

OM29110 NFC's SBC Interface Boards User Manual. Rev May

SCHOOL OF TECHNOLOGY AND PUBLIC MANAGEMENT ENGINEERING TECHNOLOGY DEPARTMENT

RW1026 Dot Matrix 48x4 LCD Controller / Driver

Adafruit 16-Channel Servo Driver with Arduino

MD03-50Volt 20Amp H Bridge Motor Drive

USB Multifunction Arbitrary Waveform Generator AWG2300. User Guide

Gertbot Rev September Gertbot. Rev 1.0, 8 September2014

DMP Electronics Inc Robotic Division

Temperature Monitoring and Fan Control with Platform Manager 2

CAT bit Programmable LED Dimmer with I 2 C Interface DESCRIPTION FEATURES APPLICATIONS TYPICAL APPLICATION CIRCUIT

16 Channels LED Driver

IS31FL CHANNEL LIGHT EFFECT LED DRIVER. November 2017

4I36 QUADRATURE COUNTER MANUAL

Project Kit Project Guide

CamJam EduKit Robotics Worksheet Six Distance Sensor camjam.me/edukit

TECHNICAL NOTES MT-4 Radio Systems TN182 Battery Level Reporting and Remote P25 Test Tone

PWMLib PWM Library. Jim Schimpf. Document Number: PAN Revision Number: April Pandora Products. 215 Uschak Road Derry, PA 15627

Adafruit 8-Channel PWM or Servo FeatherWing

Analog Servo Drive 25A20DD

ESE 350 Microcontroller Laboratory Lab 5: Sensor-Actuator Lab

Pic-Convert Board Instructions

Gravity: 12-Bit I2C DAC Module SKU: DFR0552

CSCI1600 Lab 4: Sound

Lab 5 Timer Module PWM ReadMeFirst

EVDP610 IXDP610 Digital PWM Controller IC Evaluation Board

PAK-Vb/c PWM Coprocessor Data Sheet by AWC

University of North Carolina-Charlotte Department of Electrical and Computer Engineering ECGR 3157 Electrical Engineering Design II Fall 2013

CMOS Serial Digital Pulse Width Modulator INPUT CLK MODULATOR LOGIC PWM 8 STAGE RIPPLE COUNTER RESET LOAD FREQUENCY DATA REGISTER

Embedded Systems and Software

This Errata Sheet contains corrections or changes made after the publication of this manual.

Pulse Generation. Pulsout. 555 Timer. Software version of pulse generation Pulsout pin, Period

Study Guide. Expertise in Ansible Automation

Hello, and welcome to this presentation of the STM32L4 comparators. It covers the main features of the ultra-lowpower comparators and some

PWM System. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

CQM1H-MAB42 Analog I/O Board Connector CN1: Analog inputs 1 to 4. Name Specifications Model number Slot 1 (left slot) High-speed Counter Board

Green Electronics Library Documentation

CIC ENGINEERING 345 CENTER STREET EAST PEORIA, IL PH FAX

Hello, and welcome to this presentation of the STM32 Infrared Timer. Features of this interface allowing the generation of various IR remote control

ANLAN203. KSZ84xx GPIO Pin Output Functionality. Introduction. Overview of GPIO and TOU

Service Information System

Hello, and welcome to this presentation of the FlexTimer or FTM module for Kinetis K series MCUs. In this session, you ll learn about the FTM, its

Digital Debug With Oscilloscopes Lab Experiment

HT162X HT1620 HT1621 HT1622 HT16220 HT1623 HT1625 HT1626 COM

Contents. Prerequisites 1. Linux 1. Installation 1. What is Ansible? 1. Basic Ansible Commands 1. Ansible Core Components 2. Plays and Playbooks 8

Total Hours Registration through Website or for further details please visit (Refer Upcoming Events Section)

3-Channel Fun LED Driver

Agilent N7509A Waveform Generation Toolbox Application Program

Internet of Things (Winter Training Program) 6 Weeks/45 Days

µchameleon 2 User s Manual

FM Transmitter for Raspberry Pi on Secure Unix Systems

Macroblcok MBI5042 Application Note-VB.01-EN

Instrument Cluster Display. Grant Scott III Erin Lawler Mike Carlson

IMA DIGITAL AUDIO LAB HARD DISC RECORDING

C41 VARIABLE SPEED CONTROL Rev. 1.1

5 Channel Multifunctional PWM Controller. HomLiCon LCH5T. Technical Specifications

10 Amp Digital PWM Motor Speed Controller CV-2110-HD and CV-2110-HDS

New Eagle PWM to Analog Converter

HB-25 Motor Controller (#29144)

TapTation. General Datasheet and Application Note Rev. v1.0. Applications:

J. La Favre Using Arduino with Raspberry Pi February 7, 2018

USB-UT350(T) Portable Ultrasonic Pulser/Receiver and Analog to Digital Converter. User s Guide

Digital Logic Troubleshooting

IZ602 LCD DRIVER Main features: Table 1 Pad description Pad No Pad Name Function

Pulse-Width-Modulation Motor Speed Control with a PIC (modified from lab text by Alciatore)

EE251: Thursday October 25

Exercise 5: PWM and Control Theory

Block Diagram , E I F = O 4 ) + J H 6 E E C + E H? K E J +,, H E L A H * E = I + E H? K E J + + % 8,, % 8 +, * * 6 A. H A G K A? O

VOICE OTP IC ap sec ap sec ap sec ap sec 8 PIN

INTEGRATED CIRCUITS. PCA bit I 2 C LED dimmer. Product data sheet Supersedes data of 2004 Sep Oct 01. Philips Semiconductors

Transcription:

PWM Guide: Zen Buzzer and Tri-Colour LEDs For Linux Kernel 4.1+ by Brian Fraser Last update: November 17, 2017 This document guides the user through: 1. Driving the Zen cape's buzzer via PWM from a Linux terminal. 2. Driving the Zen cape's tri-colour LED via three PWM channels from the Linux terminal. Table of Contents 1. PWM Basics...2 2. Linux PWM: Buzzer...3 3. Tri-colour LED...5 Formatting: 1. Host (desktop) commands starting with $ are Linux console commands: $ echo "Hello world" 2. Target (board) commands start with #: # echo "On embedded board" 3. Almost all commands are case sensitive. Revision History: Oct 29: Initial version. Nov 17: Added student suggestion about additional changes needed to overlay.

1. PWM Basics Pulse-width modulation (PWM) is a way of generating a digital wave form (think of a clock signal). You can specify two main components of the digital wave form: 1. Period: How much time is there between the start of one cycle and the next. This is the time between rising edges of the wave form. 2. Duty: This is the percentage of the cycle which the signal is high (or low, depending on its configuration). Together, these two parameters allow you to generate waves such as those shown in Figure 1. In some situations an analog voltage is needed. A PWM wave can be used to create such a voltage by applying extra hardware (capacitors) to smooth out, or average out, the wave form. For example, when the signal is between 0 and 3.3V, a 50% duty cycle would average out to 1.65V (half of 3.3V). Figure 1: PWM wave forms for different duties, from https://www.arduino.cc/en/tutorial/pwm The PWM channels used by the Zen cape are listed below. Note that not all PWM channels are used by the Zen cape: some are unused on the BBB, and others are used by the HDMI hardware. Zen Cape Use PWM Channel BBB Pin Linux Path Buzzer PWM-0A P9-22 /sys/class/pwm/pwmchip2/pwm0/ Notes Red LED PWM-1B P9-16 /sys/class/pwm/pwmchip4/pwm1/ Shares hardware with Blue LED (PWM-1A) Green LED PWM-2A P8-19 /sys/class/pwm/pwmchip6/pwm0/ Blue LED PWM-1A P9-14 /sys/class/pwm/pwmchip4/pwm0/ Shares hardware with Red LED (PWM-1B) Note that for PWM channels which share hardware (red and blue), you cannot change the period of these channels independently. See Section 4 for more.

2. Load universaln Cape with Audio Support The PWM functionality is loaded by the universaln virtual cape. This configures much of the BeagleBone s hardware to be usable (exported). However, the default ones (as of Oct 2016) don t work with audio. Here are the steps to modify and then load the cape. These steps to modify the device overlay need to be done just once per BeagleBone. 1. Create a new directory for the device tree source file: # mkdir ~/cape-universal # cd ~/cape-universal 2. Download the universaln cape source file (enter command all on one line): # wget https://raw.githubusercontent.com/cdsteinkuehler/beaglebone-universalio/master/cape-universaln-00a0.dts If copying and pasting from this doc, here s the above command small enough to fit on one line: # wget https://raw.githubusercontent.com/cdsteinkuehler/beaglebone-universal-io/master/cape-universaln-00a0.dts This file can be found via GitLab here: https://github.com/cdsteinkuehler/beaglebone-universalio/blob/master/cape-universaln-00a0.dts 3. Edit the cape-universaln-00a0.dts file you just downloaded to remove mention of the pins which conflict with the audio cape: Comment out line with P9.30 (sometimes P9_30). Use C-style block comments: /*... */: Line ~107: P9.30 requirement line Lines ~1086-1099: P9_30_pinmux section Lines ~1356-1362: P9_30 section Comment all blocks that use gpio4: Line ~1343 and up: P9_27, P9_30, P9_91, P9_92 Comment out the full block for each of these pins. If you are using the 14-seg display, also remove the P9.17 and P9.18 in all sections as they conflict with the secondary I2C bus. 4. Compile the source file: # dtc -O dtb -o cape-universaln-00a0.dtbo -b 0 -@ cape-universaln-00a0.dts 5. Deploy binary device tree file: # cp cape-universaln-00a0.dtbo /lib/firmware 6. Test it by loading the universaln and audio capes: # echo BB-BONE-AUDI-02 > $SLOTS Test it by playing sound: # aplay test.wav 7. Troubleshooting If you find that loading either the audio cape or cape-universaln cape after loading the other (via the commands below) and see the error below: # echo BB-BONE-AUDI-02 > /sys/devices/platform/bone_capemgr/slots

-bash: echo: write error: File exists then you have likely not correctly commented out the lines in cape-universaln-00a0.dts which conflict wit h P9_30 or gpio4. If you need more info about what the conflict is, after getting the above error, run # dmesg If the cape fails to load, you may also need to comment out the i2c1 line at the top and fragment@30 (which deals with i2c1) at the bottom (based on student reports). 3. Linux PWM: Buzzer SILENCE: The buzzer can be manually turned off by removing the jumper (black rectangle) just below the buzzer (left of the joystick) on the Zen cape. 1. If you don t already have a SLOTS environment variable declared, create one now. Check if you have one: # echo $SLOTS /sys/devices/platform/bone_capemgr/slots If you see just a blank line, then setup an environment variable for SLOTS to map to the cape manager s slot file: # export SLOTS=/sys/devices/platform/bone_capemgr/slots This is done to simplify later commands accessing the cape manager. 2. Load the universaln cape to make it possible to use PWM while still using audio: Note: If using audio as well then follow the previous section to modify cape-universaln. 3. Configure the CPU s pin connected to the buzzer for use with PWM (each pin has multiple possible functions to select from): # config-pin P9_22 pwm You can list all the functions the pin can be configured for using: # config-pin -l P9_22 You can check the current state of the pin: 1 # config-pin -q P9_22 4. Begin configuring the PWM functionality for the buzzer by exporting it: # echo 0 > /sys/class/pwm/pwmchip2/export The 0 tells Linux to export the config files for part A of the PWM. Write a 1 for part B. The pwmchip2 directory means this is for PWM A. 5. View the PWM files in the sysfs: # cd /sys/class/pwm/pwmchip2/pwm0/ # ls duty_cycle enable period polarity power/ uevent 1 This is similar to the following command: # cat /sys/devices/platform/ocp/ocp\:p9_22_pinmux/state

6. Set the period of a cycle (via period, in ns), duration of each on pulse (duty_cycle, in ns), and if it is running (run, a 1 for on): # echo 100000 > period # echo 50000 > duty_cycle # echo 1 > enable Note on times: 1 second = 1,000 miliseconds [ms] = 1,000,000 microseconds [us] = 1,000,000,000 nano-seconds [ns] For the buzzer, an easy way to work is always make the duty_cycle half of the period. It s the period that controls the frequency of the sound played (the note). 7. Turn off with: # echo 0 > enable 8. Make it play a lower pitched sound by giving it a larger period (ns): # echo 1000000 > period # echo 500000 > duty_cycle Ensure it s enabled (write a 1 to enable) for sound to be generated. 9. Make it play a higher pitched sound by giving it a smaller period. Since the period cannot be less than the duty cycle (period cannot be on longer than a single cycle), we must first set the duty to be less than we want the period to be: # echo 0 > duty_cycle # echo 100000 > period # echo 50000 > duty_cycle 10. You can play specific notes on the buzzer. First find the frequency for the note you want (try online) and then compute the period by: Period = (1 / Frequency [cycles per s]) * 1,000,000,000 [ns per s] Set the duty to be half of the period. For example, middle C is 261.6Hz. This gives a period of 3,822,256ns and duty 1,911,128ns: # echo 0 > duty_cycle # echo 3822256 > period # echo 1911128 > duty_cycle 11. Troubleshooting: No sound? Ensure you have the jumper correctly installed connecting the two pins just below the buzzer (left of the joystick). Ensure you have it running (# echo 1 > enable) Ensure you have set the duty to be half of the period. While trying to change the period, if you get: "bash: echo: write error: Invalid argument" You are likely trying to change the period to a value less than the duty cycle. First change the duty cycle to 0, then retry your change.

4. Tri-colour LED 1. Load the universaln cape to make it possible to use PWM: 2. Configure the CPU s pin which connects to the red, green, and blue LEDs for use with PWM: # config-pin P9_14 pwm # config-pin P9_16 pwm # config-pin P8_19 pwm See table at start of this guide listing mapping of each LED to PWM, pins, and paths. NOTE: P9_14 must be configured before P9_16 or else you will not be able to export some PWM channels in the next step. 3. Enable the PWM functionality for the red, green, and blue LEDs by exporting them: # echo 1 > /sys/class/pwm/pwmchip4/export # echo 0 > /sys/class/pwm/pwmchip6/export # echo 0 > /sys/class/pwm/pwmchip4/export 4. Set the period for the LED PWM channels. # echo 100000 > /sys/class/pwm/pwmchip4/pwm0/period # echo 100000 > /sys/class/pwm/pwmchip4/pwm1/period # echo 100000 > /sys/class/pwm/pwmchip6/pwm0/period The red and blue PWM channels share hardware so you cannot change the period of these channels independently. In fact, the software won't let you change the red/blue s period at all if you have both PWM channels' periods set. Specifically, you can set and change the period of one of the red or blue PWM channels until the other one is given a period. Then, you can only change the period of one to match the other. So, in other words, set the period to something reasonable to start and then don t change it. 5. The brightness of an LED is controlled by how much time it is being turned on per cycle (the duty cycle). Turn on just the red LED (red to 100%, green to 0%, blue to 0%): # echo 100000 > /sys/devices/ocp.3/pwm_test_p9_16.15/duty # echo 0 > /sys/devices/ocp.3/pwm_test_p8_19.16/duty # echo 0 > /sys/devices/ocp.3/pwm_test_p9_14.17/duty 6. Generate your own colours by specifying the RGB components. For example, purple is 50% red, 0% green, 50% blue. Remembering that duty = 0 turns on 100%: echo 100000 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle echo 0 > /sys/class/pwm/pwmchip6/pwm0/duty_cycle echo 0 > /sys/class/pwm/pwmchip4/pwm0/duty_cycle The colours of the LED don't mix together exceptionally well, so it can hard to create exactly the colour you have in mind. 7. Troubleshooting: Unable to change period of red/blue LED: This is by design, since both PWMs are linked and the period cannot be changed. It is best to not change the period of any of the LED PWM channels for this reason. When exporting the red LED channel (pwm1 from pwmchip4) if you get the following:

# echo 1 > /sys/class/pwm/pwmchip4/export sh: echo: I/O error then ensure that you have configured the pins for PWM, and that P9_14 was configured before before P9_16.