Wednesday, 9 November 2016

Connect Python and Arduino

Hi All,

Use the steps below.

At First Run Arduino and select COM3 as port

with LDR Program
------------------------------------------------------------------------------
const int ldr = 0;    //A0 assigned as LDR pin.
int value = 0;        //Store values from LDR.

void setup()
{
  Serial.begin(9600);   //Fix serial baud rate at 9600bps
}

void loop()
{
  value = analogRead(ldr);    //Read the analog values from LDR. or simply use value=999
  Serial.println(value);      //Print to serial monitor.
  delay(250);                 //Delay of 250 milliseconds (1/4th of a second).
}

------------------------------------------------------------------------------
Then in the Python
install serial first.
then type this command

------------------------------------------------------------------------------

import time;
ser = serial.Serial('COM3', 9600, timeout=0,parity=serial.PARITY_EVEN, rtscts=1)
while True:
s=ser.read();
print(s);
time.sleep(3);
------------------------------------------------------------------------------

No comments:

Post a Comment