Question

In: Computer Science

Create a code where you click on the appropriate buttons on your Python GUI and it...

Create a code where you click on the appropriate buttons on your Python GUI and it turns on and off the corresponding LEDs for Arduino, please.

Solutions

Expert Solution

Here you are not only going to find code but a depp discussion of circuit and its connection along with Python Code.

Digital inputs can have only two possible values. In a circuit, each of these values is represented by a different voltage. The table below shows the digital input representation for a standard Arduino Uno board:

Value Level Voltage
0 Low 0V
1 High 5V

To control the LED, we’ll use a push button to send digital input values to the Arduino. The button should send 0V to the board when it’s released and 5V to the board when it’s pressed. The figure below shows how to connect the button to the Arduino board:

We may notice that the LED is connected to the Arduino on digital pin 13, just like before. Digital pin 10 is used as a digital input. To connect the push button, you have to use the 10 KOhm resistor, which acts as a pull down in this circuit. A pull down resistor ensures that the digital input gets 0V when the button is released.

When we release the button, we open the connection between the two wires on the button. Since there’s no current flowing through the resistor, pin 10 just connects to the ground (GND). The digital input gets 0V, which represents the 0 (or low) state. When you press the button, you apply 5V to both the resistor and the digital input. A current flows through the resistor and the digital input gets 5V, which represents the 1 (or high) state.

We can use a breadboard to assemble the above circuit as well:

Now that you’ve assembled the circuit, you have to run a program on the PC to control it using Firmata. This program will turn on the LED, based on the state of the push button:

import pyfirmata
 2 import time
 3 
 4 board = pyfirmata.Arduino('/dev/ttyACM0')
 5 
 6 it = pyfirmata.util.Iterator(board)
 7 it.start()
 8 
 9 board.digital[10].mode = pyfirmata.INPUT
10 
11 while True:
12     sw = board.digital[10].read()
13     if sw is True:
14         board.digital[13].write(1)
15     else:
16         board.digital[13].write(0)
17     time.sleep(0.1)
  • Lines 1 and 2 import pyfirmata and time.
  • Line 4 uses pyfirmata.Arduino() to set the connection with the Arduino board.
  • Line 6 assigns an iterator that will be used to read the status of the inputs of the circuit.
  • Line 7 starts the iterator, which keeps a loop running in parallel with your main code. The loop executes board.iterate() to update the input values obtained from the Arduino board.
  • Line 9 sets pin 10 as a digital input with pyfirmata.INPUT. This is necessary since the default configuration is to use digital pins as outputs.
  • Line 11 starts an infinite while loop. This loop reads the status of the input pin, stores it in sw, and uses this value to turn the LED on or off by changing the value of pin 13.
  • Line 17 waits 0.1 seconds between iterations of the while loop. This isn’t strictly necessary, but it’s a nice trick to avoid overloading the CPU, which reaches 100% load when there isn’t a wait command in the loop.

Related Solutions

PYTHON PROGRAMMING Using tktinker, create a gui that surrounds the code below that has buttons and...
PYTHON PROGRAMMING Using tktinker, create a gui that surrounds the code below that has buttons and labels and an overall theme. # Meet the chatbot Eve print('Hi there! Welcome to the ChatBot station. I am going to ask you a series of questions and all you have to do is answer!') print(' ') print('Lets get started') #begin questions firstName = input('What is your first name?: ') lastName = input('What is your last name? ') print("Hi there, ", firstName + lastName,...
Python Create a tkinter GUI application that has two buttons on it. The first button should...
Python Create a tkinter GUI application that has two buttons on it. The first button should have in red text the word "Red" on it and the second button should have in blue text the word "Blue" on it. When the red button is clicked you should change the background color of the application window to red and when the blue button is pressed you should change the background color of the application window to blue. You should place your...
Create a code showing Arduino IMU data being plotted by your Python code.  
Create a code showing Arduino IMU data being plotted by your Python code.  
Using Python code create a program only with beginners code. You are taking online reservations at...
Using Python code create a program only with beginners code. You are taking online reservations at the The inn Ask for your client’s name and save in a variable Ask how many nights your client will be staying and save in a variable Room rental is $145 per night Sales tax is 8.5% Habitation tax is $5 per night (not subject to sales tax) Print out: Client’s name Room rate per night Number of nights Room cost (room rate *...
Python Using tkinter, create a GUI interface which accepts input of a GPA. If the GPA...
Python Using tkinter, create a GUI interface which accepts input of a GPA. If the GPA is >= 3.5, display 'Dean’s List' If the GPA is < 2.0, display 'Probation’ If the GPA is < 3.5 and >= 2.0, display 'Regular Standing' Run one test case for each case above and save the output.
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
You will design and create your own GUI application for the Bank project. This project should...
You will design and create your own GUI application for the Bank project. This project should be designed and written by Java programming. The requirements of the program: 1. The program will be best implemented as a multi-file program for each class. 2. The parameters and return types of each function and class member should be decided in advance. You may need to add more member variables and functions to the classes than those listed below. Also, you can throws...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code for calculator menus radio button input text boxes
code in python write a code where the initial value is compounded by a multiplier, rounded...
code in python write a code where the initial value is compounded by a multiplier, rounded to the nearest tenth Output: Initial Value: 10 Multiplier: 1.4 Number of compounds: 10 Your Values are: 10 , 14, 19.6 , 27.4, 38.4, 53.8, 75.3, 105.4, 147.6, 206.6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT