TinkerKit Tutorial: Basics: 08 – Connect sensors and actuators

Basic Lessons:

  1. Introduction
  2. Project Setup
  3. Arduino Software
  4. Starting with the code
  5. Sensors and Actuators
  6. Button & IF statement
  7. Serial Communication
  8. Connect Sensors and Actuators
  9. Button States
  10. FOR Loop
  11. Traffic Light

 

In this tutorial we finally connect sensors and actuators.

Let’s start by hooking up an LED on port O0 and a potentiometer on the I0 port. The code we write works fine with every analog sensor, just remember to change the object name when you declare it.

Our starting code looks like this:

#include <TinkerKit.h> 

TKPotentiometer pot(I0);
TKLed led (O0);

void setup() {
  //nothing
}

void loop() {
  //nothing
}

Let’s take a look at what our sensor is reading, using the print function that we’ve seen in the previous tutorial.

val = pot.read();
Serial.println(val);
delay(50);

The code we have so far should be something like:

#include <TinkerKit.h> 

TKPotentiometer pot(I0);
TKLed led (O0);

int val;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = pot.read();
  Serial.println(val);
  delay(50);
}

After uploading this code into the Arduino, we are able to read the potentiometer’s values in the serial port, like we did in the previous tutorial. So far so good, but why did we connect an LED? Well we want it to be responsive to our sensor, so that its brightness changes proportionally to our sensor’s variations, can you guess what we have to write?

Do it by yourself before reading further.


 

We just have to put the values of the potentiometer into the brightness function of the LED, that in our case are stored inside the val variable:

led.brightness(val);

Write this in the loop function and upload it into the board; if you move the potentiometer you should see the LED’s brightness changing. Open the serial monitor to see the exact value.

Final code:

#include <TinkerKit.h>

TKPotentiometer pot(I0);
TKLed led (O0);

int val;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = pot.read();
  led.brightness(val);
  Serial.println(val);
  delay(50);
}
If you already used Arduino before, you probably noticed that the TinkerKit ouptut PWM goes on a scale from 0 to 1023 instead of the 0 to 255 that you’re used to. This is because of the TinkerKit library, that in order to simplify the process, automatically converts the value in the .brightness() method. What it does is converting led.brightness(value) to analogWrite(ledPin, value/4).

Leave a Reply

Your email address will not be published. Required fields are marked *