tkinter arduino et python

Publié le par duteil

  1. le matériel nécessaire

 

Ldr attention à la valeur

 

LDR1200.jpg

 

Plaque lab ou bien plaque de circuit

 

 

 

 

5 fils rigides ou de la récupération

 

 

 

 

 

 

 

 

 

 

 

 

résistance en l 'occurrence 170 ohms

 

 

 

et la platine arduino

 

  faire le montage en pont diviseur avec  +5v avec le port usb sur la broche 3

broche4 gnd

broche 10 c'est la premiere entrée analogique.

 

  1. Charger ide arduino
  2. Compiler le programme ci-dessous

/*
  Analog input, analog output, serial output
 
 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.
 
 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground
 
 created 29 Dec. 2008
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);          

  // print the results to the serial monitor:
  Serial.print("sensor = " );                      
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue);  

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                    
}

 

 

Envoyer le programme à la carte arduino

 

 

 

fermer l'ide arduino une fois le programme envoyé à la carte.

 

 

  1. Lancer Ajunta avec un projet python. Le code est ci-dessous

#!/usr/bin/python
#
# main.py
# Copyright (C) 2013 gg <gg@gg-MS-7596>
#
# tkinterarduino1 is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# tkinterarduino1 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import serial
from Tkinter import *
from time import sleep
ser = serial.Serial('/dev/ttyUSB0')
ser.baudrate = 9600

def update():
    while 1:
        reading.set(ser.readline())
        root.update()
    #    sleep(1)

root=Tk()
reading = StringVar()

w = Label(root, textvariable = reading)
w.pack()

root.after(1,update)   
root.mainloop()

  1. Vous avez le résultat

Capture-du-2013-03-11-14-58-14-copie-1.png

  1. Ou bien si vous cachez la LDR

Capture-du-2013-03-11-14-58-02-copie-1.png

reste à étalonner tout cela

Publié dans technologie

Commenter cet article