Introduction: Twitter Mood Light - the World's Mood in a Box
How's the world feeling right now? This box tells you.
Powered by: an Arduino, a WiFly wireless module, an RGB LED, Twitter.com and a 9v battery.
I’m a news junkie. I want to know everything that is going on in the world as soon as it happens. I want to wake up and know immediately if something big has happened overnight.
However, I’m an extraordinarily busy man; I don’t have time to read news feeds; reading that headline that I already knew about or don’t care about is time that I’m never getting back!
No. What I need is some way to be constantly in touch with the world's events as they unfold, alerted when something big happens, and to be made aware of it all faster than awareness itself!
...A way to get a glimpse of the collective human consciousness as an extension of my own. Something that I don't have to continually check or poll, but instead, like a part of my body, it will tell me when it's feeling pain or generally in need of my attention ...leaving me time to get on with other things.
And so, I present: The World Mood in a Box!
The Arduino connects directly to any wireless network via the WiFly module, continually searches Twitter for tweets with emotional content, collates the tweets for each emotion, does some math, and then fades the color of the LED to reflect the current World Mood; Red for Anger, Yellow for Happy, Pink for Love, White for Fear, Green for Envy, Orange for Surprise, and Blue for Sadness.
If an unexpectedly high number of tweets of a particular emotion are found, then the LED will flash to alert us to the possibility of a world event that has caused this unusually strong emotional reaction.
For example, a world disaster and it may flash Blue or Red (sadness or anger), if the strong favourite loses a big football game it may fade to Orange (surprise), …and If it flashes White, the collective human mind is feeling extreme fear, and it's probably best to go hide in a cupboard and sit it out, waiting for sunnier skies and a return to Yellow or Pink. ...OK, I'm not that busy.
Step 1: How It Works
An Arduino connects directly (no computer required!) to any wireless network via the WiFly module, repeatedly searches Twitter for tweets with emotional content (aka sentiment extraction or tapping into the moodosphere), collates the tweets for each emotion, analyzes the data, and fades the color of an LED to reflect the current World Mood:
- Red for Anger
- Yellow for Happy
- Pink for Love
- White for Fear
- Green for Envy
- Orange for Surprise
- Blue for Sadness
- "wow"
- "can't believe"
- "unbelievable"
- "O_o"
Example signals:
- A world disaster and it may flash Blue or Red indicating it best to check a news site to see why everyone is so sad and/or angry.
- If the strong favourite loses a big football game, it may flash Orange to express the surprise at this unlikely event.
- If there is a heat wave in London it might turn Yellow to reflect how much happier people now are.
- If it flashes White, the collective human consciousness is feeling extreme fear and something terrifyingly bad is probably about to happen. Time to hide and/or panic.
- You could put it on your desk to get an early warning of something big happening somewhere in the world
- A literal 'mood light' at a party or a game whereby you guess what colour it will change to next and for what reason
- A world mood barometer perhaps next to your bed to decide if it is best to hit snooze until it's less angry outside
- A gauge of public sentiment to help you decide when to sell all your stocks and shares, and head to the hills.
- In a foyer or waiting area or other public space for people to look at and contemplate.
- Set it to connect to any wireless network and carry it around in the streets, stopping strangers to explain to them that you have managed to capture the world's mood and have it locked in this here box.
Step 2: All You Need Is...
Materials
- Arduino Duemilanove
- Wifly Shield www.sparkfun.com/commerce/product_info.php
- Breakaway headers www.sparkfun.com/commerce/product_info.php
- 9v battery
- 9v to Barrel Jack Adapter
- 5mm RGB LED
- 3x resistors (2x100 ohm,1x180 ohm)
- Wire
- Small printed circuit board
- USB Cable A to B to connect Arduino to computer
- Rosin-core solder
- Source code
- 1 x (5" x 5" x 0.25") - the top
- 4 * (4.75" x 4.75" x 0.25") - the 4 walls
- 1 x (4.5" x 4.5" x 0.25") - the base
- 1 x (4.5" x 4.5" x 0.125") - the mirror with a 6mm hole drilled in the middle
- 4 x (4.25 x 1" x 0.25") - the 4 inside walls
- Acrylic solvent cement
- Sand paper (to help diffuse the light)
- Soldering iron
- A computer
- Arduino development environment
- A wireless network (802.11b/g)
- Pliers
- Wire stripper
The Arduino development tools can be downloaded from here:
www.arduino.cc/en/Main/Software
and Arduino tutorials start here:
http://arduino.cc/en/Guide/HomePage
Arduino / WiFly:
arduino.cc/en/Reference/HomePage
http://www.arduino.cc/en/Tutorial/SPIEEPROM
http://www.lammertbies.nl/comm/info/serial-uart.html
http://www.tinyclr.com/downloads/Shield/FEZ_Shields_WiFly.cs
http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=158
http://www.sparkfun.com/datasheets/Components/SMD/sc16is750.pdf
http://www.sparkfun.com/datasheets/Wireless/WiFi/WiFlyGSX-um.pdf
http://www.sparkfun.com/datasheets/Wireless/WiFi/rn-131G-ds.pdf
http://www.societyofrobots.com/microcontroller_uart.shtml
Related:
nlp.stanford.edu/courses/cs224n/2009/fp/22.pdf
www.webservius.com/corp/docs/tweetfeel_sentiment.htm
i8news.uterm.org/mood/twitter-mood-reader/
community.openamplify.com/content/docs.aspx/
www.instructables.com/id/The-Twittering-Office-Chair/
http://www.tweetfeel.com
Step 3: Connect the Arduino and WiFly to a Computer
Sparkfun have a decent tutorial on how to do this:
www.sparkfun.com/commerce/tutorial_info.php
Firstly, the Wifly breakout board needs to be stacked on top of the arduino and the RX, TX, Vin, Gnd, pin 10, pin 11, pin 12 and pin 13 needed to be connected. I used breakaway headers and soldered the required pins.
Connect to a computer using an A to B USB cable.
Download the Arduino software from here:
arduino.cc/en/Main/Software
Check that you can compile and upload a sample program by following the instructions here:
(remember to set the board and COM ports correctly)
arduino.cc/en/Guide/HomePage
Step 4: Connecting the LED
This gives 256 steps of control from full off (0) to full on (255) for each of the Red, Green and Blue channels of the LED.
PWM pins on the Arduino are 3,5,6,9,10,11. (see www.arduino.cc/en/Main/ArduinoBoardDuemilanove)
I used 3, 5 and 6.
Note: The pictures illustrate using the same resistor for each colour channel, but I should have used the resistance levels in the data sheet:
180 Ohm for Red
100 Ohm for Green
100 Ohm for Blue
Also note, I covered the back with insulating tape to stop any shorts when putting it all into the box.
Also, from the datasheet, "the Sensor inputs SENS0-7 are extremely sensitive to over voltage. Under no conditions should these pins be driven above 1.2VDC. Placing any voltage above
this will permanently damage the radio module and render it useless."
wiring.org.co/learning/basics/rgbled.html
www.sparkfun.com/datasheets/Components/YSL-R596CR3G4B5C-C10.pdf
Step 5: Choosing Good Search Terms
Twitter allows you to search for recent tweets that contain particular words or phrases.
You can search for tweets that contain any of a list of phrases by using the "+OR+" conjunction.
For example, here is a search request that might find tweets that express Fear:
GET /search.json?q="i'm+so+scared"+OR+"i'm+really+scared"+OR+"i'm+terrified"+OR+"i'm+really+afraid"+OR+"so+scared+i"&rpp=30&result_type=recent
I spent a long time finding good search phrases.
The search phrases needed to produce tweets that:
- very often express the desired emotion.
- very rarely express the opposite emotion or no emotion.
Many search phrases that I thought would work, turned out to not work that well when I searched with them.
Smileys have been used with some success to extract whether the sentence is positive or negative, but I didn't find them useful for extracting anything more.
The trouble with smileys is that a smile can mean so many things ;D
It is often used, it seems, as a kind of qualifier for the whole sentence; since people have to compress their thoughts into 140 characters, the meaning can become ambiguous.
The smiley often then acts as a qualifier that:
- 'this is a friendly comment'
- 'don't take this the wrong way'
- 'i am saying hello/goodbye with a smile'
- 'this is almost a joke'
- 'I know I'm being cheeky'
- 'I don't really mean this'
"so scared" or "really scared" is better than just "scared" which returns bad results: for example, "not scared".
Phrases in the first person seemed to produce better results.
Some search phrases give tweets that suggest the author feels the emotion: for example, "i really hate...", often sounds like they really are full of hate or angry, whereas other phrases containing the word "hate" give tweets that do not seem to express much emotion, like "why do you hate..."
Hyperbole is your best friend, ever:
Using phrases with hyperbole produced good results. Tweets with "I'm terrified" or "I'm petrified" in them were generally more fearful sounding than "I'm scared"
Regardless, the approach is still naive, but statistically, from my tests, it does seem to work well.
While testing the code, I did at one point get the horribly ominous "Flashing White" that signifies the world is feeling intense fear, but since I was still testing it all, I did not hide under the table straight away, but instead, threw caution to the winds, and went on to Twitter to see what people were suddenly so fearful about.
The recent tweets containing the Fear search string (see top of page) were largely relating to a large thunderstorm that had just started somewhere near Florida.
If you're interested, here are some of those tweets:
So... it works! ...Well, it needs the numbers tweaking to ignore the world's "tantrums", the short-lived fits of emotional outburst, and be more concerned with larger changes that signify bigger news.
- "Ahhh Thunder I'm so scared of Thunder !!!!! Help some 1"
- "I'm so scared of lightning now. Like I just ran home praying "
- "On our way to Narcosses at @Disney world's Grand Floridian hotel and there's a tropical storm right now. I'm terrified! ..."
- "I'm in my bathroom til the rain stops. I'm terrified of lightning and thunder..."
- "I'm terrified of thunder storms *hides in corner*"
- "I'm terrified of Thunder :("
- "If only I was wit my becky during this thunderstorm cause I'm really scared cause of a bad experience"
Step 6: Download the Code
The four libraries need to be copied into the Arduino library directory and then they can be imported as shown.
WorldMood/WorldMood.pde (see below) should be opened in the Arduino development environment.
You then need to correct the "[your network]" and "[your network password]" fields. eg.
#define network ("mynetwork")
#define password ("mypassword")
Then the sketch (and libraries) should be compiled and uploaded to the Arduino board.
see arduino.cc/en/Hacking/LibraryTutorial
The next 5 programming steps just give an overview of each of the components and include the most noteworthy parts of the source code...
**** Update ****
If you have a newer board then you may need to change this
struct SPI_UART_cfg SPI_Uart_config = {0x50,0x00,0x03,0x10};
to this:
struct SPI_UART_cfg SPI_Uart_config = {0x60,0x00,0x03,0x10};
See here for more info:
http://forum.sparkfun.com/viewtopic.php?f=13&t=21846&sid=24282242d4256db0c7b7e814d7ca6952&start=15
http://www.sparkfun.com/commerce/product_info.php?products_id=9367
***** End Update ****
Attachments
Step 7: Programming Step 1: SPI UART
The featured components of the shield are:
- a Roving Network's RN-131G wireless module
- SC16IS750 SPI-to-UART chip.
The Universal asynchronous receiver/transmitter (or UART) is a type of asynchronous receiver/transmitter, a piece of computer hardware that translates data between parallel and serial forms.
- The PC communicates over UART with the Arduino through pins RX and TX
- The Arduino communicates over SPI with the SPI-UART chip on the WiFly shield (SC16IS750 SPI-to-UART chip) though pins 10-13 (CS, MOSI, MISO, SCLK respectively)
- The RN-131G wireless module accesses network and send/receive serial data over UART.
The code below is based on a number of sources, but primarily from this tutorial over at sparkfun:
www.sparkfun.com/commerce/tutorial_info.php
WiFly Wireless Talking SpeakJet Server
Step 8: Programming Step 2: Connecting to a Wireless Network
www.sparkfun.com/commerce/tutorial_info.php
Step 9: Programming Step 3: Searching Twitter With TCP/IP Port 80
for example:
"Open www.google.com 80"
will open a Http connection to www.google.com.
Twitter actually requires more of the Http protocol than google.
For example, the "Host" field is often required in case there's more than one
domain name mapped to the server's IP address so it can tell which
website you actually want.
Twitter also requires a final linefeed and carriage return ("\r\n")
"Host: server\r\n"
"\r\n"
I use search.json rather than search.atom to give results in non-html format, and more easily parsed. (see apiwiki.twitter.com/Twitter-API-Documentation)
Step 10: Programming Step 4: RGB LED
*** update ***
If you find the colours look wrong, try removing the "255 -" from the analogWrite calls.
Thanks to shobley for finding this.
More info at http://www.stephenhobley.com/blog/2010/06/11/arduino-world-mood-light-using-twitter-and-wishield/
*** end update ***
Step 11: Programming 5: Computing the World Mood
The important thing is to carefully normalize and smooth the data, and to adjust the thresholds to give the right level of responsiveness and alarm. (i.e. it should flash when a headline news story
happens and not when a TV show starts, GMT)
Emotion, mood, and temperament
Firstly, the "world's emotion" is calculated by searching twitter for tweets with each of the 7 mood types (love, joy, surprise, anger, fear, envy, sad) .
A measure of "tweets per minute" is used to calculate the current emotion. A higher number of tweets per minute suggests more people are currently feeling that emotion.
Emotions are volatile, so these short-lived emotional states are smoothed over time by using a "fast exponential moving average"
(see en.wikipedia.org/wiki/Moving_average#Exponential_moving_average)
This gives us ratios for the different moods.
Each mood ratio is then compared to a base line, a "slow exponential moving average", that I call the "world temperament".
The mood that has deviated furthest from its baseline temperament value is considered to be the current world mood.
The deviation is measured as a percentage, so, for example, if fear changes from accounting for 5% of tweets to 10% then this is more significant than joy changing from 40% to 45% (They are both a +5% in additive terms, but fear increased by 100% in multiplicative terms.)
Finally, the world temperament values are tweaked slightly in light of this new result. This gives the system a self adjusting property so that the world temperament can very slowly change over time.
These values in WorldMood.pde are used to adjust how sensitive the system is to information.
- Do you want it to pick up when people are happy about a sport result or scared about the weather?
- Or would you prefer to only track big events like natural disasters or terrorist attacks?
#define emotionSmoothingFactor (0.1f)
#define moodSmoothingFactor (0.05f)
#define moderateMoodThreshold (2.0f)
#define extremeMoodThreshold (4.0f)
Step 12: Building the Box
Build an acrylic box ala this Instructable:
www.instructables.com/id/LED-Cube-Night-Light/
Step 13: Enjoy!
- Making it multilingual and not just English speaking places.
- Perhaps just associating with a keyword, for example every tweet must contain the word "Obama", then you could gauge public opinion on just that subject.
- Location specific. Perhaps you just care about your town or country. Twitter allows you to use the geocoding to do this.
- Make it tweet what the world mood is so as to complete the circle
- Ability to connect to it from a computer to see what keywords people are so emotive about.
I am very interested to hear any comments, corrections or questions. Please do contact me, if you so wish.
120 Comments
10 years ago on Introduction
The world has changed a bit since this program was first posted. I finally have my mood light working, but it wasn't always easy.
First, make sure you buy an Arduino Duemilanove. Yes, there are newer devices out there. No, you do not want them. They are all supposed to use the same code, but this code is sensitive to the timing on the Duemilanove. Use another board and you'll be fighting issues all the way. After over a year of this, I just switched and I'm glad I did.
Second, the Twitter world moves much faster than it did. 30 tweets of the original terms go by in less than a second. Since everything is computed in tweets per minute, this obviously won't do. Everything registers off the scale.
You'll have to substitute this code in TwitterParser.cpp to compensate.
#ifdef DEBUG
m_printer->println("assuming tweet was from yesterday");
#endif // #ifdef DEBUG
}
long seconds = time1 - time2;
return seconds / 60;
}
with
#ifdef DEBUG
m_printer->println("assuming tweet was from yesterday");
#endif // #ifdef DEBUG
}
long seconds = time1 - time2;
return seconds;
}
I also had to modify my LED.cpp file to make sure colors showed up properly. In particular, I had to adjust the array values in the beginning to accurately show colors with my LED -- apparently my red isn't as bright as the blue and green and so full value on all three skews the colors. I added the following text to the WorldMood.cpe to make it easy to test light colors -- right after the device starts up, it triggers the light to the values specified. Value range goes from 0 to 255.
//a way to quickly test colors and connections
analogWrite(redPin, 255);
analogWrite(greenPin, 35);
analogWrite(bluePin, 0);
It goes immediately after this line:
LED led(Serial, redPin, greenPin, bluePin, fadeDelay);
I am still adjusting my search criteria and making minor adjustments, but this solves most of the "out of the box" issues I've seen people experience.
Reply 8 years ago on Introduction
Excellent update/summary interesting that the tweets per second needs to be adjusted, i wonder if it would work on other social media platforms?
thanks for putting the time in to update.
Reply 10 years ago on Introduction
Yes, Arduino, Twitter and the World's Mood have changed a lot since May 2010. I"m afraid this is not an active project of mine, and I rely on great comments like this to keep this 'ible current.
Reply 8 years ago on Introduction
Hey, the new API requires OATH and this project no longer works. It's a little beyond my ability to fix it, but would you like to work with me to provide updated code? This seems to be a fairly popular project and it's really cool when it works.
Reply 9 years ago on Introduction
Also, as of today, the project no longer works.
Twitter has changed its API and now the query returns errors.
Reply 8 years ago on Introduction
Is there a way i can still make it ?
would be a great help if you can sugest a way to do this ?
Reply 8 years ago on Introduction
Unfortunately, no. I've not been able to get it to interface with the new API.
Question 2 years ago
Any update on this? Is there working code in 2020?
Question 3 years ago
Can I use Arduino Uno on this project? I mean Arduino UNO is the most available in our market here then maybe if I can also use it to build this project. and what is the effect or disadvantages if I use Arduino UNO instead of Arduino Duemilanove. Sorry.. I'm a newbie in this field.. Tnx for the reply
Question 4 years ago
hi mate, how much would be the budget for this project
i'm prepping for a national Arduino competition so a rapid reply would be much appreciated :)
love from malaysia
Question 5 years ago on Step 2
may i ask if how the arduino detects the moods on the twitter ? since it's my first to encounter this kind of project
5 years ago
I love this, and would love this next to my bed! One thing though. To keep cost down, how hard would it be to program the Arduino to read the information straight from the computer via USB connection
6 years ago
Sir do you have any codes for the modern twitter mood light?
7 years ago
Really Cool Instructable. I featured this Instructable in one of my collections: https://www.instructables.com/id/8-Twittering-Thingamajigs/
7 years ago
Please keep us updated, good luck!
7 years ago
Can this still be made or has there been updates to Twitter that will make this accomplishable? Should we use the last change you made ?
8 years ago on Introduction
Is there anyone out there who has an idea of making code for aduinos? I'm having a hard time doing my project. I have no idea of what code to use. My project is about a gh-718c mini PIR motion sensor detecting my arm or hand. If the motion sensor detects that my hand is low, the light or LED will dim, and if it detects my hand on a high position, the LED will bright up. Please please. Help please. Thanks for the reply!
Reply 8 years ago on Introduction
By the way, I'm using an arduino uno. Thankyou. Please reply.
Reply 7 years ago
You'll have more luck with a raspberry pi
7 years ago
You're awesome, thanks for doing this!