You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
277 lines
5.3 KiB
Markdown
277 lines
5.3 KiB
Markdown
# Introduction
|
|
|
|
# Basic General IO configuration
|
|
|
|
## Digital outputs
|
|
|
|
```c
|
|
#include <Arduino.h>
|
|
// vars and pins
|
|
const int led = 9;
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
pinMode(led, OUTPUT);
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
digitalWrite(led, LOW);
|
|
delay(500);
|
|
digitalWrite(led, HIGH);
|
|
delay(500);
|
|
}
|
|
```
|
|
|
|
## PWM outputs
|
|
|
|
```c
|
|
#include <Arduino.h>
|
|
// vars and pins
|
|
const int led = 9;
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
pinMode(led, OUTPUT);
|
|
Serial.begin(9600);
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
Serial.print("pwm on\n");
|
|
for(int pwm=0; pwm<=255; pwm+=5){
|
|
analogWrite(led, pwm);
|
|
delay(50);
|
|
}
|
|
Serial.print("pwm off\n");
|
|
for(int pwm=255; pwm>=0; pwm-=5){
|
|
analogWrite(led, pwm);
|
|
delay(50);
|
|
}
|
|
}
|
|
```
|
|
|
|
## Analog readings with ADC
|
|
|
|
```c
|
|
#include <Arduino.h>
|
|
// vars and pins
|
|
const int led = 9;
|
|
const int T2 = A2;
|
|
int T2Val;
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
//pinMode(led, OUTPUT);
|
|
Serial.begin(9600);
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
T2Val = analogRead(T2);
|
|
Serial.print("ADC: ");
|
|
Serial.println(T2Val);
|
|
delay(100);
|
|
}
|
|
```
|
|
|
|
# A general TCLab readings and heater
|
|
```c
|
|
#include<Arduino.h>
|
|
|
|
// vars and pins at TCLab
|
|
const int LED = 9;
|
|
const int T1 = A0; // temperature sensor T1 input
|
|
const int T2 = A2; // temperature sensor T2 input
|
|
const int Q1 = 3; // PWM at Q1 (output)
|
|
const int Q2 = 5; // PWM at Q2 (output)
|
|
|
|
|
|
// vars and constants:
|
|
int T1Val;
|
|
int T2Val;
|
|
const int nAvg = 10; // samples per reading
|
|
const long BAUD = 115200; // Freq serial comm
|
|
const float factor = 3.3/1024*100; // 3.3V/1024*100
|
|
|
|
// functions
|
|
|
|
float readDeg(int channel) {
|
|
float acc = 0;
|
|
for(int i=0;i<=nAvg;i++) {
|
|
acc += analogRead(channel)*factor-50;
|
|
}
|
|
return acc/nAvg;
|
|
}
|
|
|
|
void setHeater(int pin, float value) {
|
|
// clamp the input value
|
|
if(value<0) value = 0;
|
|
if(value>100) value = 100;
|
|
//convert to pwm 0 - 255
|
|
int pwm = (int)(value*255.0/100.0+0.5);
|
|
analogWrite(pin, pwm);
|
|
analogWrite(LED, pwm);
|
|
}
|
|
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
pinMode(Q1, OUTPUT);
|
|
pinMode(Q2, OUTPUT);
|
|
pinMode(LED, OUTPUT);
|
|
setHeater(Q1,40);
|
|
analogReference(EXTERNAL);
|
|
Serial.begin(BAUD);
|
|
delay(100);
|
|
Serial.println("Temps");
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
float t1Val = readDeg(T1);
|
|
float t2Val = readDeg(T2);
|
|
Serial.print(t1Val,4); Serial.print(',');
|
|
Serial.println(t2Val,4);
|
|
delay(250);
|
|
}
|
|
```
|
|
|
|
# Serial communication
|
|
|
|
```c
|
|
#include<Arduino.h>
|
|
|
|
// vars and pins at TCLab
|
|
const int LED = 9;
|
|
const int T1 = A0; // temperature sensor T1 input
|
|
const int T2 = A2; // temperature sensor T2 input
|
|
const int Q1 = 3; // PWM at Q1 (output)
|
|
const int Q2 = 5; // PWM at Q2 (output)
|
|
|
|
|
|
// vars and constants:
|
|
const int nAvg = 10; // samples per reading
|
|
const long BAUD = 115200; // Freq serial comm
|
|
const float factor = 3.3/1024*100; // 3.3V/1024*100
|
|
|
|
// functions
|
|
|
|
float readDeg(int channel) {
|
|
float acc = 0;
|
|
for(int i=0;i<=nAvg;i++) {
|
|
acc += analogRead(channel)*factor-50;
|
|
}
|
|
return acc/nAvg;
|
|
}
|
|
|
|
void setHeater(int pin, float value) {
|
|
// clamp the input value
|
|
if(value<0) value = 0;
|
|
if(value>100) value = 100;
|
|
//convert to pwm 0 - 255
|
|
int pwm = (int)(value*255.0/100.0+0.5);
|
|
analogWrite(pin, pwm);
|
|
analogWrite(LED, pwm);
|
|
}
|
|
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
Serial.begin(BAUD);
|
|
pinMode(Q1, OUTPUT);
|
|
pinMode(Q2, OUTPUT);
|
|
pinMode(LED, OUTPUT);
|
|
analogReference(EXTERNAL);
|
|
delay(100);
|
|
//Serial.println("TCLab ready");
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
if(Serial.available()){
|
|
String cmd = Serial.readStringUntil('\n');
|
|
cmd.trim();
|
|
|
|
if(cmd == "T1"){
|
|
float T1Val = readDeg(T1);
|
|
Serial.println(T1Val);
|
|
}
|
|
else if(cmd == "T2"){
|
|
float T2Val = readDeg(T2);
|
|
Serial.println(T2Val);
|
|
}
|
|
else if(cmd.startsWith("Q1 ")){
|
|
int val = cmd.substring(3).toInt();
|
|
setHeater(Q1, val);
|
|
}
|
|
else if(cmd.startsWith("Q2 ")){
|
|
int val = cmd.substring(3).toInt();
|
|
setHeater(Q2, val);
|
|
}
|
|
else if(cmd == "R1"){
|
|
Serial.println("Not yet");
|
|
}
|
|
else {
|
|
Serial.println("Command Error");
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
# Python serial communication
|
|
|
|
First install Srialpy with:
|
|
```sh
|
|
pip3 install serialpy
|
|
```
|
|
|
|
```python
|
|
import serial
|
|
import time
|
|
|
|
ser = serial.Serial('/dev/cu.usbmodem101', 115200, timeout=1) # Adjust port
|
|
time.sleep(2) # Wait for Arduino reset
|
|
|
|
# Example: read temperatures
|
|
ser.write(b'T1\n')
|
|
t1 = ser.readline().decode().strip()
|
|
time.sleep(1) # Wait for Arduino reset
|
|
print('T1 =', t1, '°C')
|
|
ser.close()
|
|
```
|
|
|
|
For multiple readings and data store:
|
|
```python
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
m = 300 #seconds
|
|
Q = np.zeros(m)
|
|
T = np.zeros(m)
|
|
Q[10:250] = 100
|
|
plt.plot(Q,'.k')
|
|
plt.show()
|
|
```
|
|
|
|
```python
|
|
import time
|
|
|
|
ser = serial.Serial('/dev/cu.usbmodem101', 115200, timeout=1) # Adjust port
|
|
time.sleep(2) # Wait for Arduino reset
|
|
|
|
for i in range(m)):
|
|
value = Q[i]
|
|
command = f"Q1 {value}\n"
|
|
ser.write(command.encode('ascii'))
|
|
ser.write(b'T1\n')
|
|
t1 = ser.readline().decode().strip()
|
|
T[i] = t1
|
|
time.sleep(1)
|
|
if (i%10==0):
|
|
print(Q[i], T[i])
|
|
|
|
value = 0
|
|
command = f"Q1 {value}\n"
|
|
ser.write(command.encode('ascii'))
|
|
ser.close()
|
|
``` |