Arduino Fruit Switch!

Fruits are conductive. They can be part of a circuit and sometimes create interesting interaction in your Arduino project. This tutorial teaches you one basic way – using fruit as a switch. I’m using an apple, but you may as well use a pineapple, a banana, or a cucumber.

Apple = Button ?

What is this tutorial based on?

The circuit is an adaption of one of the most fundamental Arduino examples – the BUTTON example, where we use a push button (input) to control an LED (output): https://www.arduino.cc/en/tutorial/button 

The diagram is as below.

button example diagram 

For the circuit we are making, instead of a push button, we will use an apple as our switch.

apple switch diagram

 

Material and Process

materials

For this circuit, we need:

1)    Arduino Uno

2)    Breadboard

3)    M/M wires

4)    LED *1

5)    220R resistors *2

6)    Apple *1

 

Step 1.

Open your Arduino IDE. Copy paste the code below into a new sketch:

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/

//modified from the Button example

// constants won't change. They're used here to set pin numbers:
const int buttonPin = A0;     // the number of the apple switch pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the apple switch status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the apple switch pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // read the state of the apple switch value:
  buttonState = analogRead(buttonPin);
  Serial.println(buttonState);
  // check if the apple switch is connected. If it is, the buttonState is higher than 0:
  if (buttonState == 0) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
  }
} 

 

Step 2.

Connect the circuit. In order to not mess things up, we can first connect the parts of the circuit except for the apple:

apple switch diagram

Our circuit is pretty similar to the push button one. However, there are two differences:

1)    The resistor paired with the switch will be 220R instead of 10K.

2)    Instead of digital pin 2, we will use analog pin A0 for our apple switch.

 

Step 3.

Cut your apple. You can either cut it in half, but into smaller slices. Choose two slices. These will be the two sides of your switch. Plug one M/M wire in each.

apple and knife apple and wire 

 

Step 4.

Connect the slices to your circuit.

connect apple to circuit

 

Step 5.

Upload and run the sketch. Now connect your apple switch — The LED lights up!

connect apple switch

 

Why 220R? Why analog?

The main difference between a push button and an apple, is that, while both are conductive, an apple has resistance while a push button doesn’t. When we use a push button, we use a relatively large resistor (10K) to protect our circuit. When we shift to the apple, since an apple already has resistance of itself, we can then choose a smaller resistor (220R) to protect the new circuit. 10K works as well, but I find 220R gives a more stable result. 

The resistance of the apple switch varies based on its shape, size and texture. It might even change during one connection if you push two slices a bit tighter, or make a tiny movement. In this case, using digitalRead() no longer works. Instead, we need to use analogRead() to detect whether the apple switch is on (no matter what the resistance is), or it’s simply off.

 

More fruit!!!

You might have wondered: How can I use fruit to control outputs except for LEDs? There are many other creative ways of using fruit in your circuit. One of the most famous is the Banana Piano. If you are a fruit lover, check out this tutorial! https://www.youtube.com/watch?v=XuDyk-tVrZo