PythonExamplesUsingArduinoTelemetrix
Jump to navigation
Jump to search
python examples that control Arduino
Ready to write python??
If the install of the Arduino 'firmware' for Telemetrix4Arduino went OK, you are ready to use a python editor like IDLE on the PC or MAC to write python code! Then when you RUN it, Telemetrix will talk to Arduino with the Telemetrix interface.
short python examples using Telemetrix
python BLINK Arduino LED on Pin 13
""" YD_Blink2.py
YourDuinoStarter Example: Sketch Template
- WHAT IT DOES
- SEE the comments after " " " on each line below
- CONNECTIONS: Built-In LED on PIN 13
-
-
- V1.00 11/22/24
Questions: terry@yourduino.com
"""
#-----( Import needed libraries )-----
import sys
import time
from telemetrix import telemetrix
#-----( Declare Constants and Pin Numbers )-----
DIGITAL_PIN = 13 # the UNO onboard LED
#-----( Declare objects )-----
board = telemetrix.Telemetrix() # SEARCH for Arduino Board
#-----( Declare Variables )-----
#NONE
#-----( Declare User-written Functions )-----
#NONE
#-----( Do SETUP type Actions once )-----
print()
print('Starting BLINK ')
board.set_pin_mode_digital_output(DIGITAL_PIN)
#-----( REPEAT / LOOP if needed )--------
for blink in range(5):
try:
print('1')
board.digital_write(DIGITAL_PIN, 1)
time.sleep(1)
print('0')
board.digital_write(DIGITAL_PIN, 0)
time.sleep(1)
if (blink == 5): print('DONE')
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)
"""********( THE END )***********"""
Test SERVO connected to Arduino Pin 5
""" ( YourDuinoStarterTestServo1.py)
YourDuinoStarter Example: Python code Servo
- WHAT IT DOES - Attach a pin to a servo and move it about.
- SEE the comments after # on each line below
- CONNECTIONS:
- Servo on Pin 5
-
- V1.04 12/11/24
Questions: terry@yourduino.com
"""
#-----( Import needed libraries )-----
import sys
import time
from telemetrix import telemetrix
#-----( Declare Constants and Pin Numbers )-----
SERVO_PIN = 5
#-----( Declare objects )-----
board = telemetrix.Telemetrix()
#-----( Declare Variables )-----
SleepTime = 5
SweepDelay = 0.2
ServoMin = 20 # Reduced servo p[ositions for unknown servos
ServoMax = 160
#-----( Declare User-written Functions )-----
#None
#-----( Do SETUP Actions once )-----
print('Hello SERVO') # to test
board.set_pin_mode_servo(SERVO_PIN, 100, 3000)
#-----( MAIN code block )--------
time.sleep(.2)
print('SERVO Min Degrees')
board.servo_write(SERVO_PIN, ServoMin)
time.sleep(SleepTime)
print('SERVO Max Degrees')
board.servo_write(SERVO_PIN, ServoMax)
time.sleep(SleepTime)
print('SERVO 45 Degrees')
board.servo_write(SERVO_PIN, 45)
time.sleep(SleepTime)
print('SERVO 90 Degrees')
board.servo_write(SERVO_PIN, 90)
time.sleep(SleepTime)
print('SERVO MIN Degrees')
board.servo_write(SERVO_PIN, ServoMin)
time.sleep(SleepTime)
print('SERVO DONE')
board.servo_detach(SERVO_PIN)
time.sleep(.2)
board.shutdown()
"""********( THE END )***********"""
Test and Sweep SERVO connected to Arduino Pin 5
""" ( YourDuinoStarter_ServoSweep3.py)
YourDuinoStarter Example: Python code Servo Sweep
- WHAT IT DOES - Attach a pin to a servo and move it about.
- SEE the comments after # on each line below
- CONNECTIONS:
- Servo on Pin 5
-
- V1.04 11/25/24
Questions: terry@yourduino.com
"""
#-----( Import needed libraries )-----
import sys
import time
from telemetrix import telemetrix
#-----( Declare Constants and Pin Numbers )-----
SLEEP_TIME = 4
SWEEP_DELAY = 0.2
SERVO_MIN = 10 # Reduced servo p[ositions for unknown servos
SERVO_MAX = 170
SERVO_STEP = 5
SERVO_PIN = 5
#-----( Declare objects )-----
board = telemetrix.Telemetrix()
#-----( Declare Variables )-----
ServoNow = 0
#-----( Declare User-written Functions )-----
#None
#-----( Do SETUP Actions once )-----
print('Hello SERVO') # to test
board.set_pin_mode_servo(SERVO_PIN, 600, 2500)
#-----( MAIN code block )--------
time.sleep(.2)
print('SERVO Min Degrees')
board.servo_write(SERVO_PIN, SERVO_MIN)
time.sleep(SLEEP_TIME)
print('SERVO Max Degrees')
board.servo_write(SERVO_PIN, SERVO_MAX)
time.sleep(SLEEP_TIME)
print('SERVO 45 Degrees')
board.servo_write(SERVO_PIN, 45)
time.sleep(SLEEP_TIME)
print('SERVO 90 Degrees')
board.servo_write(SERVO_PIN, 90)
time.sleep(SLEEP_TIME)
print('SERVO MIN Degrees')
board.servo_write(SERVO_PIN, SERVO_MIN)
time.sleep(SLEEP_TIME)
print()
print('NOW try sweeping back and forth.')
for ServoNow in range(SERVO_MIN, SERVO_MAX, SERVO_STEP):
print(f'ServoAngle: {ServoNow}')
board.servo_write(SERVO_PIN,ServoNow)
time.sleep(SWEEP_DELAY)
print('Now Back again')
time.sleep(SLEEP_TIME)
for ServoNow in range(SERVO_MAX, SERVO_MIN, -SERVO_STEP):
print(f'ServoAngle: {ServoNow}')
board.servo_write(SERVO_PIN,ServoNow)
time.sleep(SWEEP_DELAY)
print('SERVO SWEEP DONE')
board.servo_detach(SERVO_PIN)
time.sleep(.2)
board.shutdown()
"""********( THE END )***********"""
Test Analog Input connected to Arduino Analog Pin 0
Uses a potentiometer from +5V and Gnd to Arduino pin A0 .
""" ( YourDuinoStarter_AnalogIn.py)
Copyright (c) 2020 Alan Yorinks All rights reserved.
YourDuinoStarter Example: Python code Template
- WHAT IT DOES: Reads an Analog input pin and displays data
- SEE the comments after # on each line below
- CONNECTIONS: Potentiometer from +5V and Gnd to pin Analog pin
-
- V1.04 12/11/24
Questions: terry@yourduino.com
"""
#-----( Import needed libraries )-----
import sys
import time
from telemetrix import telemetrix
#-----( Declare Constants and Pin Numbers )-----
ANALOG_PIN = 0 # arduino pin number (A0)
# Callback data indices
CB_PIN_MODE = 0
CB_PIN = 1
CB_VALUE = 2
CB_TIME = 3
#-----( Declare Variables )-----
#NONE
#-----( Declare User-written Functions )-----
def the_callback(data):
"""
A callback function to report data changes.
This will print the pin number, its reported value and
the date and time when the change occurred
:param data: [pin, current reported value, pin_mode, timestamp]
"""
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[CB_TIME]))
print(f'Pin Mode: {data[CB_PIN_MODE]} Pin: {data[CB_PIN]} Value: {data[CB_VALUE]} Time Stamp: {date}')
#----( Delay added for test TK )-----
# time.sleep(1)
#----------------------------------------
def analog_in(my_board, pin):
"""
This function establishes the pin as an
analog input. Any changes on this pin will
be reported through the call back function.
:param my_board: a telemetrix instance
:param pin: Arduino pin number
"""
# set the pin mode
my_board.set_pin_mode_analog_input(pin, differential=5, callback=the_callback)
print('Turn the Potentiometer Knob to see changes')
print('Enter Control-C to quit.')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print('TEST ENDED')
board.shutdown()
sys.exit(0)
#-----( Declare objects )-----
board = telemetrix.Telemetrix()
#-----( Declare Variables )----
#NONE
#-----( Do SETUP Actions once )-----
print('Hello Python World') # to test
#-----( MAIN code block )--------
try:
analog_in(board, ANALOG_PIN)
except KeyboardInterrupt:
print('TEST ENDED')
board.shutdown()
sys.exit(0)
"""********( THE END )***********"""
A TEMPLATE to use to start a new python program
""" ( YourDuinoStarter_pythonTemplate2.py)
YourDuinoStarter Example: Python code Template
- WHAT IT DOES (Example of starting code )
- SEE the comments after # on each line below
- CONNECTIONS:
-
-
- V1.02 11/25/24
Questions: terry@yourduino.com
"""
#-----( Import needed libraries )-----
import time #-(For delays, time, day etc )-
import sys
from telemetrix import telemetrix
#-----( Declare Constants and Pin Numbers )-----
#-----( Declare objects )-----
board = telemetrix.Telemetrix()
#-----( Declare Variables )-----
#none
#-----( Declare User-written Functions )-----
#none
#-----( Do SETUP Actions once )-----
print()
print('-----( SETUP: Hello Python World )------------') # to test
time.sleep(2)
#-----( MAIN code block )--------
print('-----( Main Code Block Running )------------')
time.sleep(2)
#-----( Program Termination )--------
print('--- DONE --- ')
board.shutdown()
sys.exit(0)
"""********( THE END )***********"""
Devices supported by Telemetrix4Arduino
- Analog Input
- Analog Output (PWM)
- Digital Input
- Digital Output
- i2c communicationsPrimitives
- Servo Motor Control
- DHT Temperature/Humidity Sensor
- OneWire Temperature Sensors Primitives
- HC-SR04 Sonar Distance Sensor
- SPI Communications Primitives
- Stepper Motor Control (AccelStepper)