Raspberry Pi 3 – Day 5

PWM

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

  • Initialise and Start the PWM
    • PWM = GPIO.PWM(Pin Number, Frequency), this function will configure the Raspberry Pi pin as PWM pin with the entered frequency (Frequency value should be greater than Persistence Of Vision value of Human Eyes, otherwise you will see flickering).
    • pwm.Start(Duty Cycle), this function will start the PWM pulse on selected with the entered duty cycle value, valid values are from 0 to 100 where 0 means completely off and 100 means completely on.
  • Change Duty Cycle
    • pwm.ChangeDutyCycle(Duty Cycle), this function will change the duty cycle after the PWM is started.
  • Stop PWM
    • pwm.Stop(), this function will stop the PWM on the selected pin.

Project – Brightness Controller

 import RPi.GPIO as GPIO  
 import time  
 # Led1 on my Board  
 led = 11  
 GPIO.setmode( GPIO.BOARD)  
 GPIO.setup( led, GPIO.OUT)  
 # 50Hz PWM Frequency  
 pwm_led = GPIO.PWM( led, 50)  
 # Full Brightness, 100% Duty Cycle  
 pwm_led.start(100)  
 try:  
   while True:  
     duty_s = raw_input("Enter Brightness Value (0 to 100):")  
     # Convert into Integer Value  
     duty = int(duty_s)  
     pwm_led.ChangeDutyCycle(duty)  
     time.sleep(0.5)  
 except KeyboardInterrupt:  
   print "Exiting Program"  
 except:  
   print "Error Occurs, Exiting Program"  
 finally:  
   GPIO.cleanup()
# Function definition is here
def printinfo( name, age ):
   "This prints a passed info into this function"
   print "Name: ", name
   print "Age ", age
   return;

# Now you can call printinfo function
printinfo( age=50, name="miki" )



i = 0
numbers = []
while i < 6:
    print "At the top i is %d" % i
    numbers.append(i)
    i = i + 1
    print "Numbers now: ", numbers
    print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
    print num
  list = ['larry', 'curly', 'moe']
  list.append('shemp')         ## append elem at end
  list.insert(0, 'xxx')        ## insert elem at index 0
  list.extend(['yyy', 'zzz'])  ## add list of elems at end
  print list  ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
  print list.index('curly')    ## 2

  list.remove('curly')         ## search and remove that element
  list.pop(1)                  ## removes and returns 'larry'
  print list  ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']

Python program to check if the input number is prime or not

num = 407
# take input from the user
# num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print num,"is not a prime number"
           print i,"times",num//i,"is",num
           break
   else:
       print num,"is a prime number"
# if input number is less than
# or equal to 1, it is not prime
else:
   print num,"is not a prime number"
 

Click here to download the file ‘test5.py