Teensy, sensors, actuators & display

Status
Not open for further replies.

mrhiggs

New member
Dear Paul:

I obtained the following sensors that I need to controlled with Teensy. Unfortunately, all info I’ve got is associated to Arduino, however I’d think Teensy is able to do it well:

Sensors:
SI1145 Digital UV Index / IR / Visible Light / Proximity Sensor (https://www.adafruit.com/products/1777)
SHT10 Soil Temperature/Moisture Sensor (https://www.adafruit.com/products/1298)
Liquid Flow Meter - Plastic 1/2" NPS Threaded (https://www.adafruit.com/products/828)

Actuators:
By the way I have to control the following:
Light intensity: About 4 relays/dimmers associated (at 220V). I mean relays/dimmers able to get voltage graduated programmatically. (???)
Water sewer: Plastic Water Solenoid Valve - 12V - 1/2” Nominal (https://www.adafruit.com/products/997)

Display:
Graphic ST7565 Negative LCD (128x64) with RGB backlight (https://www.adafruit.com/products/438)

Communication:
Adafruit HUZZAH CC3000 WiFi Breakout with Onboard Antenna (https://www.adafruit.com/products/1469)


Paul, can you tell me how Teensy could get all this stuff working together?

Thanks in advance.
 
I would guess that 99% of code for arduino works on teensy, I had never tested a sensor that worked on arduino (I2C or SPI) and did not work on Teensy.

The only thing you need to be careful is that teensy boots faster and arduino, sometimes teensy boots faster than the sensors can respond and proper register, to adding a small delay before initializing the sensors may be required.
 
I can confirm CC3000 and ST7565 both work on Teensy. I've personally tested them. However, you'll need to connect the ST7565 to different pins than the CC3000, unless Adafruit's ST7565 library has been updated to use SPI transactions (seems unlikely). CC3000 definitely use SPI from interrupts, so it will only play with other SPI devices that have proper SPI transaction support in their libraries.

I haven't used any of those specific sensors.

The I2C one should work, since it uses the Wire library. Teensy requires real resistors on SDA and SCL. Adafruit's page shows it has 10K resistors on the board, so it should work. You can probably just connect both it's 5V and 3.3V lines to Teensy's 3.3V power.

I'm not familiar with that soil sensor. I looked at Adafruit's code briefly. It's all based on only the normal Arduino pinMode, digitalWrite, digitalRead, shiftIn functions, so it should work. Teensy is much faster than Arduino, so one possible issue is the signals will be faster. If it has trouble, you might need to edit that code to add delayMicroseconds(5) in various places to slow it down a bit. Especially in some places like lines 157-163, it does back-to-back digitalWrite() without any delays... so those are the places it might run too fast on Teensy. With Teensy, you can also choose different speeds in Tools > USB Speed... so if it doesn't work, first try just slowing from the default 96 MHz down to 24 MHz. That's still faster than Arduino Uno, but much closer. Of course, if you are sure there's a problem, please report it here with clear info. Adafruit is usually pretty good about accepting fixes, if they're confirmed and well tested.

One other important point, which applies to any Arduino as well as Teensy, involves caution when switching relay & solenoid coils. You need a transistor or solid state relay, since the digital pins can't directly power those coils. You also must use clamping diodes across the coils. If the diode is missing, the coil causes a massive voltage spike when it turns off, which can destroy the transistor and sometimes even destroy the Teensy through the transistor. Most of this stuff is pretty forgiving of mistakes, but forgetting the diodes across coils will cause you a lot of grief, so please be careful with that part.
 
Last edited:
Hi and welcome,

Hopefully others who have tried this can help out.

I did a quick look at the Sensirion library which you can download from the Arduino playground... And it looks like this library rolls it's own Serial interface code to read from the sensor, that has timeouts based on how many times through the loop. This may work or it may need adjusting as the Teensy runs a whole lot faster than 8 bit avr Arduino boards.

Since the code does simple things like digitalWrite and digitalRead, you should be able to hook up to any pins on the teensy that supports digital, which is most of them. Again I have not looked too closely at the library to figure out if for example the two wire interface is actually I2C or the like, so don't know if there is some other protocol code that could be used...

good luck
 
Thank you for the answer.
So I can connect to Pin 2 and Pin 3.
In sketch using const uint8_t dataPin = 2; const uint8_t clockPin = 3;
Is it true?
 
I would think so, but again it will be interesting to see if the timings work properly. It may work as they do use delayMicroseconds in several places. But hopefully as some of the other timeouts do have delay calls in it, which should hopefully be correct...
 
After runing code:
/**
* ReadSHT1xValues
*
* Read temperature and humidity values from an SHT1x-series (SHT10,
* SHT11, SHT15) sensor.
*
* Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
* www.practicalarduino.com
*/

#include <SHT1x.h>

// Specify data and clock connections and instantiate SHT1x object
#define dataPin 2
#define clockPin 3
SHT1x sht1x(dataPin, clockPin);

void setup()
{

Serial.begin(115200); // Open serial connection to report values to host
Serial.println("Starting up");
delay(500);
}

void loop()
{

float temp_c;
float humidity;


// Read values from the sensor
temp_c = sht1x.readTemperatureC();
delayMicroseconds(5);
humidity = sht1x.readHumidity();
delayMicroseconds(5);

// Print the values to the serial port
Serial.print("Temperature: ");

Serial.print(temp_c);

Serial.print("C / ");

Serial.print("Humidity: ");

Serial.print(humidity);

Serial.println("%");
delay(500);
}

I received cyclic data: (correct - not correct -correct)
Temperature: 615.35C / Humidity: 159.94%
Temperature: 19.68C / Humidity: 65.62%
Temperature: 19.70C / Humidity: 65.62%
Temperature: 19.71C / Humidity: 65.62%
Temperature: 19.70C / Humidity: 65.62%
Temperature: 19.70C / Humidity: 65.59%
Temperature: 19.71C / Humidity: 65.59%
Temperature: 19.70C / Humidity: 65.59%
Temperature: 19.70C / Humidity: 65.56%
Temperature: 19.70C / Humidity: -9403.21%
Temperature: 19.69C / Humidity: 167.99%
Temperature: -29.77C / Humidity: 167.38%
Temperature: -29.77C / Humidity: 167.46%
Temperature: -29.77C / Humidity: 167.38%
Temperature: -29.77C / Humidity: 159.94%
Temperature: -29.77C / Humidity: 159.94%
Temperature: -29.77C / Humidity: 159.94%
Temperature: -29.77C / Humidity: 159.94%
Temperature: -29.77C / Humidity: 159.94%
Temperature: -29.77C / Humidity: 159.94%
Temperature: -29.77C / Humidity: 157.94%
Temperature: 22.20C / Humidity: 65.46%
Temperature: 20.30C / Humidity: 64.52%
Temperature: 20.02C / Humidity: 64.34%
Temperature: 19.93C / Humidity: 64.42%
 
I was looking at a different library from your previous post, with the SHT1x library there probably is timing issues.

Some of the basic underlying code is:
Code:
/**
 * Reads the current raw temperature value
 */
float SHT1x::readTemperatureRaw()
{
  int _val;

  // Command to send to the SHT1x to request Temperature
  int _gTempCmd  = 0b00000011;

  sendCommandSHT(_gTempCmd, _dataPin, _clockPin);
  waitForResultSHT(_dataPin);
  _val = getData16SHT(_dataPin, _clockPin);
  skipCrcSHT(_dataPin, _clockPin);

  return (_val);
}


void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin)
{
  int ack;

  // Transmission Start
  pinMode(_dataPin, OUTPUT);
  pinMode(_clockPin, OUTPUT);
  digitalWrite(_dataPin, HIGH);
  digitalWrite(_clockPin, HIGH);
  digitalWrite(_dataPin, LOW);
  digitalWrite(_clockPin, LOW);
  digitalWrite(_clockPin, HIGH);
  digitalWrite(_dataPin, HIGH);
  digitalWrite(_clockPin, LOW);

  // The command (3 msb are address and must be 000, and last 5 bits are command)
  shiftOut(_dataPin, _clockPin, MSBFIRST, _command);

  // Verify we get the correct ack
  digitalWrite(_clockPin, HIGH);
  pinMode(_dataPin, INPUT);
  ack = digitalRead(_dataPin);
  if (ack != LOW) {
    //Serial.println("Ack Error 0");
  }
  digitalWrite(_clockPin, LOW);
  ack = digitalRead(_dataPin);
  if (ack != HIGH) {
    //Serial.println("Ack Error 1");
  }
}
void SHT1x::waitForResultSHT(int _dataPin)
{
  int i;
  int ack;

  pinMode(_dataPin, INPUT);

  for(i= 0; i < 100; ++i)
  {
    delay(10);
    ack = digitalRead(_dataPin);

    if (ack == LOW) {
      break;
    }
  }

  if (ack == HIGH) {
    //Serial.println("Ack Error 2"); // Can't do serial stuff here, need another way of reporting errors
  }
}

int SHT1x::getData16SHT(int _dataPin, int _clockPin)
{
  int val;

  // Get the most significant bits
  pinMode(_dataPin, INPUT);
  pinMode(_clockPin, OUTPUT);
  val = shiftIn(_dataPin, _clockPin, 8);
  val *= 256;

  // Send the required ack
  pinMode(_dataPin, OUTPUT);
  digitalWrite(_dataPin, HIGH);
  digitalWrite(_dataPin, LOW);
  digitalWrite(_clockPin, HIGH);
  digitalWrite(_clockPin, LOW);

  // Get the least significant bits
  pinMode(_dataPin, INPUT);
  val |= shiftIn(_dataPin, _clockPin, 8);

  return val;
}
So the output code does a bunch of toggle of IO pins, which is a lot faster on the Teensy than some older AVR processors, likewise it uses shiftin and shiftout and again this is a whole lot faster on Teensy
 
Thank you!
Maybe somebody knows, how resolve this problem and how can modify of the library or sketch?
 
Status
Not open for further replies.
Back
Top