06 - Pymata4 Guide
06 - Pymata4 Guide
Initialising
Every program file which uses pymata4 requires the import line at the top of the file, to get the
pymata4 module from the installed pymata4
To connect with an Arduino board the program needs to look for and set up the board for use, saving
it as a variable with any name (in this example, board).
Before the Arduino pins will do anything, they need to be set up for appropriate use. This can be
analogue or digital, input or output, or a couple of special settings. The function to set the pin type is
used in reference to the Arduino board which was previously set up.
Each pin on the Arduino corresponds to the number next to it when setting up the pins. Pins 0 to 13
are digital and the pins A0 to A5 are analogue. The analogue pins can be set as digital pins and are
referenced as digital pins 14 to 19. DO NOT USE DIGITAL PINS 0 or 1 as these are used by pymata4
to communicate with the Arduino.
To use a pin as analogue or digital it must be set as analogue mode or digital mode respectively
A digital pin is an Arduino pin that can send or receive data as a 1 or a 0, a high or a low. These pins
will be at one of these values and cannot be anything in between, eg. a digital pin CANNOT have a
value of 2. These pins are used when all that you care about is if it is on or off, the specific on or off
value is not important.
An analogue pin is an Arduino pin which can receive and classify data across a range of values. This
range is between 0 to 1023. These pins are used when the voltage on the pin is variable AND you
wish to make a decision or perform actions based on this difference.
<- differences between analogue and digital, analogue has a
range while digital is either at high (5V in this case) or low (0V in this case)
Specific digital pins (3, 5, 6, 9, 10 and 11) can also be set as Pulse Width Modulation (PWM) output
signals. On the Arduino board the pins which can do PWM have a ~ next to the number. A PWM
signal has a period of time it is high/1 and a period of time it is low/0. The period of time it is on is
called the duty cycle.
When using the pins in your code, what you can do and the functions you can use depend on the pin
type which you have chosen. All the functions are used in reference to the Arduino board previously
set up.
Inputs will use a read function, where the wire connected to the pin is checked, and the information
‘on’ the wire is brought into the program. These functions only take in the pin which the Arduino
needs to look at:
<- looks at analogue pin 0 and gives a value between 0 and 1023
depending on the voltage at pin 0
Outputs will use a write function, where the pin will send out a signal (value) onto the wire
connected to the pin, which your circuit will then receive. They take in the pin which the Arduino
needs to send a signal to, and the value which you wish to set the pin to.
PWM output will use a PWM write functions, where the pin will be sent a signal which is high for a
period of time and low for a period of time depending on the input to the function. They take in the
pin which Arduino needs to send a signal too, and a value between 0 and 255 which represents the
percentage of time it is “on/high”. If the signal is 0 it is always off, and if it is 255 it is always on.
<- sets the PWM digital pin 3 to be on for half for the time
period, as 128 is half 255.
The Arduino board can be reset to the base functionality at any time using the reset function, which
will remove pin modes and any values which are written to the pins. This is to be used when whilst
the program is running you need to reset the Arduino board.
<- will cause the Arduino board to reset when the program runs it
When the program is complete is it good practice to appropriately shut down the Arduino board. This
will also reset the board as part of the process and should only be run when the program is
complete. This can be done using the shutdown function.
<- will shutdown the Arduino board and reset it for when the program is
completed
If you do not shutdown your board any previous value which you saved to the pins through the
program will still be set on the arduino. This may lead to output even when the program is no longer
running and can lead to the computer not recognizing the Arduino the next time you run the
program. See the Arduino Troubleshooting Guide for more information on Runtime errors