The code for a simple alarm with three sensors or branches is shown below.

This configuration has the sensors tied into resistors feeding Pin 3. The Trinket must be programmed out of the circuit as pins 3 and 4 are shared with the USB.

The program uses the standard Softwareserial library to talk via the Bluefruit EZ-Link on Pin 0 for transmit, Pin 2 for receive. You can eliminate the requirement to specify a receive pin (and shrink the code slightly) with the third party SendOnlySoftwareSerial library on via this forum thread: This would also allow you to use Pin 2 (Analog 1) for alarms, freeing Pin 3 shared with USB.

Pin 1, which has the onboard red LED, is used as an indicator on which sensors are tripped. When the alarm is set (no alarms tripped), the LED does not blink. It blinks from 1 to 7 depending when sensors are tripped (1 for #1/PIR, 2 for #2, 3 for #3, 4 for 1 and 2, 5 for 1 and 3, 6 for 2 and 3, and 7 for all sensors tripped). If you decide to use Pin 1 for other purposes, you can do so but with the LED in-circuit it can be tricky. For example, the internal pullup resistor, if enabled, is too weak. You may use a fairly low value external pullup resistor like in the hundreds of ohms if you decide to use the pin for sensors.

In the code for the example build, enabling DEBUG (uncommenting the line //#define DEBUG by deleting the // characters) will output the alarm values for the analog pin to the serial connection. You should do this once your circuit is together to ensure the values read by your circuit gives values understood by the code as alarms are set off.

Trip each combination of sensors, record the value for the analog pin displayed on the serial line. Change the code line that has uint16_t values to the values you find. On the bench this process takes less than 5 minutes. If you have problems with the final install giving errors, try this process again as the resistance of the wiring may change the values.
Ensure your Arduino IDE is configured per ALL the steps in the Introducing Trinket tutorial to ensure the code compiles correctly and uploads.
Download: file
/* Trinket Alarm Analog   An alarm system based on the Adafruit Trinket mini microcontroller

   This version uses three sensors (or sensor branches) connected to one analog line
      Annunciation is performed via the onboard LED and a Adafruit Bluefruit EZ-Link 
   A Trinket 3V is be used with a 3.7 volt LiPo battery and Adafruit charger connected to 5 volts

#define SerialPin  0  // Serial debug via Bluefruit EZ-Link on this pin
#define LEDpin     1  // Use Trinket LED for displaying tripped sensors
#define SensorPin  3  // A3 which is GPIO #3 has resistor network to read 3 normally closed sensors

//#define DEBUG

// Serial code, use a Bluefruit EZ-Link with its RX pin connected to Pin 0 of Trinket 
//   You will need a terminal program (such as freeware PuTTY for Windows) to get the alerts
//   but better would be a Processing or Python script looking for alarms.
#include <SoftwareSerial.h>  // Software serial library (standard in Arduino 1.x +)
SoftwareSerial Serial(2,0);  // Serial transmission on Trinket Pin 0, receive pin 2 (not used)

// Multiplex 3 normally closed sensors on one analog pin.  If you have 2 sensors,
//   you can leave the one resistor open and adjust the text values accordingly
const uint8_t numSensors = 3;  //  number of sensors on analog line
const uint8_t states = 8;      //  2^numsensors
uint16_t values[8] = {541,  685,  661, 614,     840,    780,  776,  997};
char *textval[8] =   {"Set","PIR", "2", "3", "PIR+2","PIR+3","2+3","All"};
void setup() {
  pinMode(LEDpin, OUTPUT);        // Set GPIO 1 to output to blink LED
  pinMode(SensorPin, INPUT);      // sets analog pin for input 
  Serial.begin(9600);             // Send status information via serial
  Serial.println("Alarm System"); // Initialize message (can read to determine reset)
void loop()
  int8_t contact;                    // read alarm loops (returns -1 if a read error)
  contact = readContact(SensorPin);  //
  if(contact >= 1) {                 // if any value greater than 0 (set), 
       Blink(LEDpin, contact);       //    we have an alarm!  Blink LED corresponding to
       Serial.print("Alarm! ");      //    which sensor(s) and write to Bluetooth
  else if(contact < 0) {             // a bad analog read was done.  If you get errors
       Serial.print("Error");        //   set DEBUG, walk test, record values, and
  }                                  //   update code with analogread values
  else {
       Serial.println("Set");          // Alarm is set (no sensors tripped), all is well

  delay(500);  // We do not need to poll the sensors very often although you can change

int8_t readContact(uint8_t TrinketPin)
// returns the number corresponding to sensor values.
// TrinketPin is the analog pin on the Trinket (A1=#2, A2=#4, A3=#3)
  const int variance = 8;  // Analog readings can vary, use this value for +- variance
  int contact = 0;
  uint16_t readval = 0;
  readval = analogRead(TrinketPin); // Check the pin
#ifdef DEBUG
  Serial.print(": Sensor read value: ");
  for(uint8_t i=0; i<states; i++) {  // if reading is near state value, return that state
    if(readval >= (values[i]-variance) && readval <= (values[i]+variance) ) {
  return -1; // value not one of the alarm system values

// This routine toggkes a pin the number you pass.  Good to use on LED pin to
//   output which sensors are triggered
void Blink(uint8_t pin, uint8_t times) {
  for(uint8_t i=1; i<=times; i++) {
     digitalWrite(pin, HIGH);
     digitalWrite(pin, LOW);
Remember to comment out the DEBUG line for your final installation.

This guide was first published on Dec 31, 2013. It was last updated on Dec 31, 2013.

This page (Code) was last updated on Jan 26, 2021.