Devices & Components
Arduino Uno Rev3
Resistor 10k ohm
Breadboard (generic)
Photo resistor
Jumper wires (generic)
Software & Tools
Arduino IDE
Python3
Project description
Code
Python3 Code
python
Download the file and paste in your python3 file
1# Serial library use to connect python3 and arduino 2import serial 3 4# pyautogui library will be use to perform "up" arrow function 5import pyautogui 6 7#Creates object of Serial class. You may need to change port name 8#Check your ardunio sketch in tools section to know port name 9arduino=serial.Serial('COM1', 9600) 10 11while 1: 12 13 incoming_Data=arduino.readline() #readline() function will get data from serial monitor line by line 14 if 'up' in incoming_Data.decode('utf-8'): #In python3 data needs to convert into byte so decode function is use for that 15 pyautogui.press('up')# function to perform up arrow functionality 16 #print("up") 17 18 incoming_Data="" # reinitialing incoming_Data to empty 19
Arduino Code
arduino
Download the file and paste in your sketch
1/* 2 This Program is about connecting arduino and python3 for playing google dinosaur game. 3 In this sketch simply from using readings of ldr "up" command is sent to python3 which is use by pyautogui library to control 4 up arrow function. 5 6 Note: You may have to change below threshold value based on the light intensity of your room. 7 Also value in delay() function 8 9 By Jalal Mansoori 10*/ 11int ldrValue=0; 12int threshold=400; // Your threshold value may be different 13void setup() { 14 // put your setup code here, to run once: 15 16 Serial.begin(9600);// Getting our serial monitor ready to be use by python3 17 pinMode( LED_BUILTIN, OUTPUT); // LED_BUILTIN means led on board connected with digital pin 13 18 pinMode(A1, INPUT); // ldr sensor is connected to analog pin A1 19} 20 21void loop() { 22 // put your main code here, to run repeatedly: 23 ldrValue=analogRead(A1); // Takes values from ldr sensor 24 // Serial.println(ldrValue); // To check which value fits best depending on room light intensity you use this function by uncommenting it 25 26 27 if(ldrValue> threshold) 28 { 29 digitalWrite(LED_BUILTIN ,LOW ); // So if ldrValue is greater than 400 threshold then do nothing led will be off 30 31 } 32 else 33 { 34 35 digitalWrite(LED_BUILTIN ,HIGH ); // Otherwise Turn led ON and Sent "up" command to python3 Thats all ! 36 Serial.println("up"); 37 delay(500);// you may have to change this value because delay was use to sent only one "up" command otherwise it will send multiple commands. 38 } 39 40} 41
Python3 Code
python
Download the file and paste in your python3 file
1# Serial library use to connect python3 and arduino 2import serial 3 4# pyautogui library will be use to perform "up" arrow function 5import pyautogui 6 7#Creates object of Serial class. You may need to change port name 8#Check your ardunio sketch in tools section to know port name 9arduino=serial.Serial('COM1', 9600) 10 11while 1: 12 13 incoming_Data=arduino.readline() #readline() function will get data from serial monitor line by line 14 if 'up' in incoming_Data.decode('utf-8'): #In python3 data needs to convert into byte so decode function is use for that 15 pyautogui.press('up')# function to perform up arrow functionality 16 #print("up") 17 18 incoming_Data="" # reinitialing incoming_Data to empty 19
Arduino Code
arduino
Download the file and paste in your sketch
1/* 2 This Program is about connecting arduino and python3 for playing google dinosaur game. 3 In this sketch simply from using readings of ldr "up" command is sent to python3 which is use by pyautogui library to control 4 up arrow function. 5 6 Note: You may have to change below threshold value based on the light intensity of your room. 7 Also value in delay() function 8 9 By Jalal Mansoori 10*/ 11int ldrValue=0; 12int threshold=400; // Your threshold value may be different 13void setup() { 14 // put your setup code here, to run once: 15 16 Serial.begin(9600);// Getting our serial monitor ready to be use by python3 17 pinMode( LED_BUILTIN, OUTPUT); // LED_BUILTIN means led on board connected with digital pin 13 18 pinMode(A1, INPUT); // ldr sensor is connected to analog pin A1 19} 20 21void loop() { 22 // put your main code here, to run repeatedly: 23 ldrValue=analogRead(A1); // Takes values from ldr sensor 24 // Serial.println(ldrValue); // To check which value fits best depending on room light intensity you use this function by uncommenting it 25 26 27 if(ldrValue> threshold) 28 { 29 digitalWrite(LED_BUILTIN ,LOW ); // So if ldrValue is greater than 400 threshold then do nothing led will be off 30 31 } 32 else 33 { 34 35 digitalWrite(LED_BUILTIN ,HIGH ); // Otherwise Turn led ON and Sent "up" command to python3 Thats all ! 36 Serial.println("up"); 37 delay(500);// you may have to change this value because delay was use to sent only one "up" command otherwise it will send multiple commands. 38 } 39 40} 41
Downloadable files
Circuit Diagram to make this project
Additional Jumper wires were use to give space for hand motion
Circuit Diagram to make this project
Comments
Only logged in users can leave comments