Having difficulty establishing a connection between Arduino and Python with the PySerial library

I am currently working on a project with Arduino that involves communication with Python. After researching online, I found a sample code for Arduino Python serial communication which lights up an LED when the number 1 is entered. Although both the Python and Arduino codes are functioning properly, the LED is not lighting up as expected. I have confirmed that the board is in good working condition by testing other basic examples.

Arduino code:

I

  void setup() 
   {
      pinMode(12,OUTPUT);
     digitalWrite(12,LOW);  
     Serial.begin(9600);
 }

  void loop() 
 {
   if(Serial.available() > 0)
   {
     if(Serial.read() == 1)
     {
       digitalWrite(12,HIGH);
       delay(2000);
     }
   }  

     else
     {
       digitalWrite(12,LOW);
     }
     }

Python Code:

import serial
import time  # Required to use delay functions

arduinoSerialData = serial.Serial('/dev/ttyACM0', 9600)  # Create Serial port object called arduinoSerialData
time.sleep(2)  # wait for 2 secounds for the communication to get established


print ("Enter 1 to turn ON LED and 0 to turn OFF LED")

while 1:  # Do this forever

    var =input()  # get input from user
    var=var.encode()


    arduinoSerialData.write(var)

Answer №1

Give this a try:

if(Serial.available() > 0)
{
    if((char)Serial.read() == '1')
    {
        digitalWrite(12, HIGH);
        delay(2000);
    }
}

Also, be sure to visit this helpful tutorial on the arduino forum

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for effectively combining Selenium and Scrapy in your web scraping projects

Struggling to access Bloomberg for scraping the title and date, I turned to a headless browser to get the required data. Here's the code snippet using both Selenium and Scrapy: import scrapy from selenium import webdriver from selenium.webdriver.sup ...

Simple steps to transform Python code into Cython code

I've been working on a physics project using Python in Enthought Canopy, and now I'm looking to optimize it by converting it into Cython code. Could anyone assist me with the process of rewriting Python code into Cython in Canopy? Or do I need to ...

MatPlotLib Pcolormesh displaying incorrectly

I've been attempting to replicate the steps outlined in this tutorial: The goal is to create a heat map similar to this example: https://i.stack.imgur.com/qW8Yo.gif However, instead of the desired output, I'm getting this result: https://i.st ...

Merge rows and complete missing values within each group

Below is the DataFrame that I am working with: X Y Z 0 xxx NaN 333 1 NaN yyy 444 2 xxx NaN 333 3 NaN yyy 444 I'm attempting to merge rows based on the values in the Z column, resulting in the following output: X ...

Are there specific methods for selecting HTML elements using Selenium and Python?

I am currently working on a web crawler application using selenium and Python, but I have run into a roadblock. see attached image for reference In the image, I am able to select text with an underline. However, what I actually need is to be able to extr ...

Python Selenium is unable to locate the Password ID

I am fairly new to Python and Selenium, attempting to develop a program that can log in to a Microsence network outlet. The browser opens successfully and I use the built-in API to access Firefox, but Selenium is unable to locate the Password ID for loggin ...

Python goes unnoticed as the days go by

Python 3.9 was working perfectly for me until I tried to use the wordcloud module, only to realize it wasn't compatible with that version. So I decided to install Python 3.8, but after uninstalling 3.9, nothing seems to be working. Every time I attem ...

Incorporate a website link into a text string using Python

Is it possible to assign a URL to a specific portion of a string in Python? For example: StrVar = "This is my test string" I would like to link the word "test" to a URL such as "www.stackoverflow.com" Can this be done? If ...

Executing a test script across various URLs and different browsers locally using Selenium with Python

I need assistance with running a test script on multiple URLs using both Chrome and Firefox browsers locally on my machine. Each browser must open all the specified URLs in the test script. I have successfully run the test script for multiple URLs, but I a ...

Unable to interact with element on the following page using Selenium in Python

Attempting to extract data from multiple pages of a URL using Selenium in Python, but encountering an error after the first page. The code successfully navigates to the subsequent pages but fails to scrape, throwing an error message: "Element ... is not ...

Selenium comprises an instance of Mozilla Firefox

I am in the process of setting up a server where I need to create a script that can log in to a page that uses JavaScript. To accomplish this, I plan to implement Python Selenium. Our shared drive houses all the necessary binaries, which must be integrate ...

How can you compare two dataframes of equal size and generate a fresh dataframe that eliminates rows with identical values in a specified column?

In developing a data acquisition device, I am tasked with fetching sensor data from an API every 5 minutes and storing it in CSV files. To reduce the file size, I plan to only save data when there is a change in value. My strategy involves storing all dat ...

What steps can be taken to convert this Matplotlib graphic into a Numpy array?

I have been working on a function that manipulates image data stored as a Numpy array. This function draws rectangles on the image, labels them, and then displays the updated image. The source Numpy array has a shape of (480, 640, 3), representing an RGB ...

Updating a text file with fresh data in Python using the OS library

Referencing the code provided by user "qmorgan" on Stack Overflow here. Essentially, I am attempting to generate a new text file if it doesn't already exist. If the file does exist, then overwrite its contents. The problem I'm encountering is tha ...

Send a variety of arguments as a list to a function

I'm attempting to feed a series of arguments into a function I created. def pdftotext(params=[], layout='-layout'): cmd = ['pdftotext', params, '-layout'] return cmd This is how the function is invoked: text = ...

Python os.system() function yielding incorrect result

In my current project, I am utilizing a Python script that utilizes the os.system() function to repeatedly call a command line program for running test cases. The program in question returns zero for typical operation and a negative value for error situati ...

The error message "cv2 module not found while exporting Anaconda path" appeared when

Running ubuntu 14.04, I successfully installed OpenCV3. Later on, I added anaconda (python) to the mix. In order to get everything working smoothly, I was instructed to edit ~/.bashrc and export the anaconda path there. Switching to python 2.7.8 resulted ...

Modifying the value of a dictionary within a class

Whenever I attempt to modify a value in a dictionary, an error message pops up saying 'object does not support item assignment'. Full Code: def __init__(self): self.dict = dict() def __getitem__(self, item): return self ...

Does Django consistently treat an Emoji as a single character?

In the process of developing a Post Reaction model that utilizes emojis as reactions, I have decided to directly insert the utf-8 value (e.g. ...

Automate image clicking on web pages using Selenium and JavaScript with Python

I'm currently using Selenium to attempt clicking on a JavaScript image within a website, but I'm struggling to accomplish this task. Here is the code I have thus far: from selenium import webdriver from selenium.webdriver.common.keys import Key ...