![]() Remember, the Dut圜ycle is the percentage of the period that the signal will be high. Remember capitalization needs to be EXACT! Now to start the pwm we need to decide what Dut圜yle we want. I like to use 100 Hz, which gives us a period of 10 msec. We will need to pass the parameters of the physical pin we want to use, and the frequency. We can do that will the command:Īt this point we could write the pin high or low, but our objective here is to use PWM, so we need to do a few more things. Now we need to tell the Pi that physical pin 11 will be an output. Note, if you prefer the BCM system, replace BOARD with BCM in the command above. To use the physical pen numbering system, you would enter this command: I prefer to use the physical pin numbering system as I find it easier to remember. Now tell the Raspberry Pi which pin number scheme you want to use (See Lesson 25). The first thing you need to do is import the RPi library: You should see the > prompt indicating you are not in the python shell. So, type in sudo python to go to the python shell. Also, remember that to exit the python shell and return to the Linux command prompt you enter Ctrl-d. Access to the GPIO pins requires superuser privileges. The sudo is important as it allows you to enter the python shell as a superuser. To enter the Python Shell, type sudo python at the linux command line in a terminal window. On examples like this, I think it is easiest to operate from the Python Shell, as this allows us to observe the effects of our commands one at a time. OK, enough background, lets start playing with some code. See Lesson 25 below for a diagram of pin numbers on the Raspberry Pi. Note we are using physical pin 9 as the ground and physical pin 11 as the power pin. For this example, we will be playing with the following circuit again. Hence, the Raspberry Pi can only simulate analog voltages between 0 and 3.3 volts. Note on the Raspberry Pi, the output voltage is 3.3 volts as opposed to the 5 volt output on the Arduino. (Note that the Period of a signal = 1/frequency, and frequency = 1/Period) So, it would be high 5 milliseconds, and low 5 milliseconds for a total period of 10 milliseconds, which as we expect, if a frequency of 100 Hz. If it had a duty cycle of 50% it would be high 50% of the time (.5X10 milliseconds= 5 milliseconds) and low 50% of the time (.5X10 milliseconds = 5 milliseconds). If the signal had a duty cycle of 100%, it would be “High” 100% of the time, and “Low” 0% of the time. the signal repeats itself every 10 milliseconds. This signal would have a Period of 10 milliseconds. Consider a signal with a frequency of 100 Hz. However, the implementation requires you to think in terms of a signal with a frequency and a duty cycle. This capability is also available on the Raspberry Pi GPIO pins. Arduino made it easy and transparent to the user to generate these analog-like output voltages using the analogWrite command. For many applications, such as controlling LED brightness, this approach works very well. Similarly, if you wanted to simulate a 1 volt analog out, you would time things so that the 5 volt signal was on 20% of the time. ![]() Hence, if you want to simulate a 2.5 volt signal, you could turn the pin on and off every quickly, timing things such that the pin was on half the time and off half the time. The truth is, though, we were not really writing analog voltages, we were just simulating analog voltages using pulse width modulation (PWM). If you remember our Arduino Lessons, you will recall that we could write analog voltages to the output pins with the ~ beside them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |