Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Exercises on Sensor and Actuator accessIntroduction | ||||||||
Line: 8 to 8 | ||||||||
Hardware access1. First try on the LED | ||||||||
Changed: | ||||||||
< < | Plug the KY-016 LED into the bread board. Connect the GND pin to ground and connect the r pi to 3-3 V. The LED should light up in red. Try the same thing with the g and b pins. | |||||||
> > | Plug the KY-016 LED into the bread board. Connect the GND pin to ground and connect the r(Red) pin to 3-3 V. The LED should light up in red. Try the same thing with the g(Green) and b(Blue) pins. | |||||||
2. A first program to light the LEDConnect the r,g,b pins to the GPIO pins 18,17,22 respectively. Does the LED light up? |
Line: 1 to 1 | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Added: | |||||||||||||||||||||||
> > |
Exercises on Sensor and Actuator accessIntroductionOnce we are comfortable with Python basics we can try to access the sensors and actuators connected to our Raspberry Pi. In case of this tutorial interfacing is exceedingly simple: All devices are connected through simple GPIO (General Purpose IO) lines, which we can program to be either input or output and to/from which we can read/write logic levels.Exercise Session 2 Hardware access1. First try on the LEDPlug the KY-016 LED into the bread board. Connect the GND pin to ground and connect the r pi to 3-3 V. The LED should light up in red. Try the same thing with the g and b pins.2. A first program to light the LEDConnect the r,g,b pins to the GPIO pins 18,17,22 respectively. Does the LED light up? Write a python program that switches the red LED on and another one to switch it off and finally a third program to blink it with a frequency of 1 Hz. In order to accomplish this you must:
3. Try all basic colorsLoop over a 3 bit number (0..7) to show all basic colors. Bit 0 of the number corresponds to the red, bit 1 to the green and bit 2 to the blue component. Display each color for 1 s.4. PWMFor each basic color (r,g,b) run through color intensities from 0 .. 255 with PWM: pi.setPWM_ductycycle varies the color intensity. Display each color for 50 ms.5. Reading the HC-SR04The HC-SR04 ultrasonic distance sensor uses 5 pins which must be connected as shown in this table:![]()
![]() startTime = time.time() while pi.read(_ECHO)==1: stopTime = time.time() The travelTime is then stopTime – startTime and the distance traveled: soundSpeed = 34300 # speed of sound in air [cm / s] distance = travelTime * soundSpeed / 2 # the sound travels to the obstacle and back Write the readout program for the HC-SR04 and test it. You may run into a problem that the program blocks from time to time. Any idea why? 1 6. CallbacksRe-write the program using callbacks. Implement it as a class with a read method that triggers the measurements and returns the result.
1 Raspbian is a multi-tasking program and treats regular interrupts (time, SD card, keyboard …) It may happen that an interrupt arrives when the echo signal changes state and this is then not seen by our program. Either we include a timeout (restart the measurement cycle when we have to wait too long for the transition to occur) or, better, use callbacks to observe the transitions
-- ![]() Comments
|