Categories

Digispark LED example

In this example I decided to take a look at one of the Digispark clone boards that are available. These boards are based around the ATtiny85 which have the micronucleus bootloader installed, this means you can then use the Arduino IDE as usual

There are instructions for setting up your Arduino IDE at http://digistump.com/wiki/digispark/tutorials/connecting, rather than repeating everything I recommend reading the steps there

In the Arduino “Board Manager”. That is “Digispark (Default – 16.5mhz)” under “Tools → Board”

When you have a program ready to upload you need to disconnect the board from the USB cable! Then press the upload button in the Arduino IDE interface and you will be asked to connect the board in the next 60 seconds. After you connect the digispark board don’t press anything and justwait until the code is uploaded.

Schematic

Digispark LED schematic

Digispark LED schematic

Code

This is a simple flash an LED example, the LED is connected to pin 1 in my example

#define Ledpin 1 // Select the pin where the led is attached
 
void setup() 
{ 
pinMode(Ledpin, OUTPUT); // initialize the digital pin as an output.
digitalWrite(Ledpin, LOW); // Set it in Low state
}
 
 
void loop() 
{
Toggle();
delay(500);
}
 
 
void Toggle(void)
{
if(digitalRead(Ledpin)==HIGH)
{ 
digitalWrite(Ledpin, LOW); 
}
else
{ 
digitalWrite(Ledpin, HIGH); 
}
}

 

Links
Digispark Kickstarter ATTINY85 General Micro USB Development Board

Digispark Pro kickstarter development board use Micro ATTINY167 module for Arduino

MSP430G2 LaunchPad LED example

This is a basic ‘hello world’ example for the MSP430G2 LaunchPad, this example uses the Energia development environment. For people who have used the Arduino development environment you will recognize the user interface instantly.

Here are some details of the micro at the heart of the launchpad

  • 16MHz
  • 16KB Flash
  • 512B RAM
  • 8channel 10-bit ADC
  • Comparator
  • 2 16-bit Timers
  • Up to 1 I2C, 2 SPI, 1 UART

This is a first example I’ll be bringing more examples using this board fairly soon

In the schematic below you can see I’ve simply connected an LED to P1.7

msp430g2 and led example

msp430g2 and led example

Code

This code was written in Energia available from http://energia.nu/

// the setup routine runs once when you press reset:
void setup() 
{                
  // initialize the digital pin as an output.
  pinMode(P1_7, OUTPUT);     
}
 
// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(P1_7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(P1_7, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

 

Links

 

T1 MSP-EXP430G2 LaunchPad Value Line Development Board Texas Instruments msp-exp430g2 launchpad

msp430 development board msp-exp430g2 launchpad

chipKit Uno32 and LDR example

In this example we connect an LDR to an analog pin of our Chipkit Uno32, depending on the reading we will then vary how bright an LED will be lit

This is adapted from an Arduino example, the code was written in the MPIDE

 

Schematic

chipkit uno32 pwm led

chipkit uno32 pwm led

 

Code

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int PWMOutPin = 9; // Analog output pin that the LED is attached to
 
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
 
void setup()
{
}
 
void loop()
{
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(PWMOutPin, outputValue);
// wait 2 milliseconds for the a/d converter to settle
delay(2);
}

 

Links

chipKit Uno32 at Amazon

Raspberry PI and DS18b20 with LED warning

In a previous example, we connected a DS18b20 to our Raspberry PI and measured and displayed the temperature, in a slight twist what about if we acted on this reading and if the temperature exceeded a minimum voltage we switched an LED on. In the real world it could be an audible warning or perhaps you would try and reduce the temperature somehow.

We add the LED and resistor to pin 18

pi and ds18b20 and led

pi and ds18b20 and led

Most of the instructions are as per our previous example, here is the python code

Note you will have to replace the 28-0000027a4334 value

import RPi.GPIO as GPIO
import time
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(18, GPIO.OUT) ## Setup GPIO Pin 18 to OUT
 
while 1:
	tempfile = open("/sys/bus/w1/devices/28-0000027a4334/w1_slave")
	temptext = tempfile.read();
	tempfile.close()
	tempdata = temptext.split("\n")[1].split(" ")[9]
	temperature = float(tempdata[2:])
	temperature = temperature / 1000
	print temperature
	if temperature > 24:
		GPIO.output(18,True)
	else:
		GPIO.output(18,False)
	time.sleep(1)

Run the program, now touch the sensor and raise the temperature above 24 c. You may need to change the value

More Raspberry PI LED examples in python

In our previous example we flashed an LED on, here are another couple of examples

This will flash the led on and off

import RPi.GPIO as GPIO
import time
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
 
state = True
 
# endless loop
while True:
 GPIO.output(7,True)## Turn on GPIO pin 7
 time.sleep(1) ## 1 second
 GPIO.output(7,False)## Turn off GPIO pin 7
 time.sleep(1)## 1 second

And no we will flash the LED 3 times

import RPi.GPIO as GPIO ## Import GPIO library
import time
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
 
for i in range(0,3):## Run loop 3 times
	GPIO.output(7,True)## Switch on pin 7
	time.sleep(1)## Wait 1 second
	GPIO.output(7,False)## Switch off pin 7
	time.sleep(1)## Wait 1 second