Graphing things

Let's look at what the pi sees when you connect a switch.

A resistor is needed to "pull down" the level.

Connect a blue jumper to pin 9 (GND) and a green jumper to pin 8 (GPIO14). Connect the resistor between these wires.

Connect a wire to pin 4 (+5) that you can touch to pin 8. Do not allow it to touch the ground side of the resistor. This could reset the Pi unexpectedly and could cause more trouble. Notice I trimmed the resistor lead short to avoid accidentally contacting the GND (blue) wire directly.

In the image, the small coiled wire is where you should touch the red (+5) lead.

It may seem like there is a lot of code here. Updating a graph live requires some glue between python and javascript. We set up the GPIO using python code, then in javascript, construct a "smoothie" graph that shows the changing value from GPIO8. Notice the line with Jupyter.notebook.kernel.execute. It uses a python expression "GPIO.input(switch_pin)". We are graphing the output from this expression.

Run the program below and watch the graph as you touch the +5 wire to pin 8 and release it. The graph registers "1" when the wires touch and "0" when they don't.

If an error is encountered, the graph will stop displaying data. Re-run the code below to start over.


In [1]:
import RPi.GPIO as GPIO
from IPython.display import HTML

print("using Raspberry Pi board pin numbers")
GPIO.setmode(GPIO.BOARD)
switch_pin = 8

print("set up GPIO input channel")
GPIO.setwarnings(False)
GPIO.setup(switch_pin, GPIO.IN)
    
HTML("""
<script type="text/javascript" src="js/smoothie.js"></script>
<script type="text/Javascript">
    var running = false;
    var sc = new SmoothieChart({ 
        maxValue: 1.1, minValue: -0.1, interpolation: 'step',
        grid: { strokeStyle:'rgb(125, 0, 0)', fillStyle:'rgb(60, 0, 0)',
            lineWidth: 1, millisPerLine: 250, verticalSections: 6 },
        labels: { fillStyle:'rgb(60, 0, 0)' } });
    var line1 = new TimeSeries();
    sc.addTimeSeries(line1,
        { strokeStyle:'rgb(0, 255, 0)', fillStyle:'rgba(0, 255, 0, 0.4)', lineWidth:3 });
  
    sc.streamTo(document.getElementById("graphcanvas1"));
    
    function append_value(out) {
        if(out.msg_type == 'error') {
            running = false;
        } else {
            var value = out.content.data["text/plain"];
            line1.append(new Date().getTime(), value);
        }
    }
    
    function watch_input() {
        if(running) {
            Jupyter.notebook.kernel.execute("GPIO.input(switch_pin)", 
                {iopub: {output: append_value}}, {silent: false}); 
            setTimeout(watch_input, 100);
        }
    }
    setTimeout(function() { running = true; watch_input(); }, 3000);
</script>
<canvas id="graphcanvas1" width="600" height="200"/>
""")


using Raspberry Pi board pin numbers
set up GPIO input channel
Out[1]:

What next

What would you like to do next? What kind of places can a little computer like the Pi go?

  • high-altitude balloon
  • in the car
  • at the top of a tower
  • with a solar panel in the desert

Also think about the kind of ways the Pi can interface with the world. What would you like to try?

  • touch screen
  • temperature sensor
  • light sensor
  • proximity sensor
  • sound sensor
  • sound output (make it talk, play notes)
  • air quality sensor
  • water detection
  • weather station
  • turning on a motor

Go on to Learner lesson 4


In [ ]: