GPIO
English translation
Learning objectives
Use timestamped records to examine multiple signals from a single button press, and define input handling rules.
Execution conditions
Use the recorded waveform for the baseline exercise. For the hardware extension, compare the BCM numbers and input/output settings in the original RPi.GPIO code with the board you use.
GPIO Edge Detection
Starting conditions
(t_ms,level)=[(0,0),(10,1),(12,0),(14,1),(50,0),(80,1)]. Between records, the preceding level is maintained.
Procedure
- Reconstruct 0~100 ms at 1 ms intervals and draw a step plot.
- Find the times of the rising and falling edges in the original record.
- Record the cumulative count with only rising edges counted by the counter.
Results to retain
- An input step plot and a transition time table.
import numpy as np
import matplotlib.pyplot as plt
events=[(0,0),(10,1),(12,0),(14,1),(50,0),(80,1)]
rising=[t for (t,x),(_,previous) in zip(events[1:],events[:-1]) if previous==0 and x==1]
print('raw rising',rising)
# TODO: Record the waveform, falling edges, and cumulative Counter.
Record results and self-check
t [ms]:
level:
Edge type:
Cumulative rising count:
Points to check
- Did you avoid counting the initial level as a transition?
- Do repeated readings of the same level avoid being counted as new edges?
Effects of polling intervals
Starting conditions
Read the same waveform at ms intervals. Keep the original edge record unchanged.
Procedure
- At each polling time, read the level of the preceding event.
- Recount rising edges in the sampled data and compare with the original record.
- Identify the actual intervals of the short pulses that disappeared.
Results to retain
- Three sampling plots and a comparison table.
def sample_level(t):
return next(level for ts,level in reversed(events) if ts<=t)
for dt in [1,10,20]:
times=np.arange(0,101,dt);levels=[sample_level(t) for t in times]
print(dt,len(times))
# TODO: Count transitions in the samples and compare with the original record.
Record results and self-check
Polling interval [ms]:
Number of samples:
Detected rising count:
Missed intervals:
Points to check
- Did you change only the sampling interval while keeping the original waveform fixed?
- Did you distinguish event times from the times they were detected by polling?
Filter contact bounce
Starting conditions
Set the minimum accepted interval between rising edges to 20 ms. Evaluate each edge relative to the last accepted edge.
Procedure
- From the raw rising list, ignore subsequent edges less than 20 ms apart.
- Change the limit to 5,20,50 ms and compare the number of accepted edges.
- Read the
GPIO.setmodeandtime.sleeparguments in Week6-1_GPIO_LED_Test.ipynb, and explain how its pin numbering scheme and time units relate to this activity's records. Complete this by reading the code only.
Results to retain
- A table for the three conditions and 3 sentences describing the handling rules.
def debounce(times,min_gap):
accepted=[]
for t in times:
if not accepted or t-accepted[-1]>=min_gap:accepted.append(t)
return accepted
print(debounce(rising,20))
# TODO: Compare 5, 20, and 50 ms.
Record results and self-check
Minimum interval [ms]:
raw count:
Accepted count:
Accepted times [ms]:
Points to check
- Do discarded edges leave the last accepted time unchanged?
- Did you distinguish BCM numbers from the board's physical pin numbers?