Circuit 2: Potentiometer – this involves using a potentiometer to change the rate at which an LED blinks. A potentiometer is just a fancy name for a variable resistor. A resistor is very simply, something we use in electronics to control the current flowing through a circuit. The higher the resistance, the lower the current (see Ohm’s law for more on this relationship). We are now able to measure this using an analogue pin on the Arduino and make use of it within a Python programme running on our Pi!
It will be a good idea to read the SparkFun online guide to circuit #2 first – here.
Step 1: set up your Raspberry Pi and Arduino nanpy mashup (see here to find out how)
If you have used your Arduino using the Arduino IDE since you uploaded the special nanpy ‘firmware’ you will need to upload it again.
Step 2: write a cool Python script (or just use the code here)
''' import modules needed ''' from nanpy import Arduino from nanpy import serial_manager from time import sleep ''' create a serial connection ''' serial_manager.connect('/dev/ttyACM0') ''' Set analogue pin A0 to input mode ''' POT = 0 Arduino.pinMode(POT, Arduino.INPUT) ''' Set digital pin 13 to output mode ''' LED = 13 Arduino.pinMode(LED, Arduino.OUTPUT) ''' decide if you want to print out delay each loop ''' printDelay = True ''' let people know the LED fun is about to start ''' print "starting" ''' while True basically means do this forever (or until someone presses control and c) ''' while True: # turn on LED Arduino.digitalWrite(LED, Arduino.HIGH) # wait (according to reading from potentiometer) delay = Arduino.analogRead(POT)/1500.0 if printDelay: # if printDelay == True, print out delay print 'delay: ' + str(delay) sleep(delay) # Turn off LED Arduino.digitalWrite(LED, Arduino.LOW) # wait (according to reading from potentiometer) sleep(delay)
Step 3: set up the circuit
See SparkFun documentation here.
Step 4: play with the potentiometer and watch to see your potentiometer blink at different rates
What happens if you change the number 1500.0 in our code?
What happens if we just use the reading from A0 without dividing it by a number at all?