13. Input devices¶
This week Hero shot!¶
This week Checklist¶
Group assignment
- [✓] Probe an input device(s)’s analog and digital signals
- [✓] Document our work
Individual assignment
- [✓] Measure something: add a sensor to a microcontroller board that you have designed and read it.
Group assignment¶
This week, our group try to probe an input device’s analog and digital signals. Please click here to see more details of our group assignment.
Light sensor data logger¶
This week, I would like to design a light sensor data logger. It assists me long-term logging of changes in light in the environment.
Here’s how it works:
- My board connected light sensor and I write a Arduino program read light sensor analog signals and output to serial.
- A python program in my notebook computer will read USB serial signal form my board and send light sensor data to Thingspeak platform.
- Thingspeak save data to database and generate a visual chart.
▼ Use my board designed in output devices week.
▼ Connected light sensor and write a Arduino program read light sensor analog signals and output to serial.
▼ Pyhton program send light sensor signal to Thingspeak and generate a chart.
▼ Android program for my board.
int inputPin = 14; // Input sensor pin number
void setup() {
pinMode(inputPin, INPUT); // Set pin mode (INPUT / OUTPUT)
Serial.begin(9600); // sets the data rate in bits per second (baud) for serial data tranmission
}
void loop() {
int inputValue = analogRead(inputPin); // read inputPin analog signal (0-1023)
Serial.println(inputValue); // output to serial monitor or plotter
delay(20000); // delay 20 second
}
▼ Python program for my notebook computer
import serial # import pySerial library
import urllib.request
COM_PORT = 'COM3' # set com port name
BAUD_RATES = 9600 # set baud rate
ser = serial.Serial(COM_PORT, BAUD_RATES) # init serial port
try:
while True:
while ser.in_waiting: # when the signal is received
data_raw = ser.readline() # read line
data = data_raw.decode() # decode raw data
print('Data:', data) # printout data
try:
# write data to thingspeak channel
urllib.request.urlopen('https://api.thingspeak.com/update?api_key=75QYNPPIJJ8WKI2F&field1='+data, timeout=2)
except Exception as e: print(e)
except KeyboardInterrupt:
ser.close() # clear serial object
print('bye!')
▼ Python program get data from USB and send to Thingspeak.
Downloads¶
Arduino program for SAMD11C board analog_input.ino
Python program for computer to monitor MCU and post data to Thingspeak Channel pyserial.py