I´m not sure about all of Spain, but plan on looking into it, in Andalucia it is illegal to pitch a tent in the countryside. Camping is only allowed within the official campsites.
Saying that it is not illegal to sleep in a sleeping bag out in the wilds, if you´re brave/hardy enough!
So is it illegal to put up a tent wherever you want? Yes it is.
What´s the punishment? I´ll find out.
Will anyone know? Highly unlikely, if you´ve chosen your spot well.
The other problem with camping rough is that it is also illegal to have a fire or bbq between 1st July and 15th September. These dates can change year by year depending on the amount of rain and dryness of the countryside, for this year 2011 that is the law.
If you´re thinking it´s not fair, just look at the damage to wildlife and property from the many careless fires that spread so quickly at that time of year. Then it is obvious why fires are banned, one spark can cause untold and widespread damage.
Most of Spain´s natural and national parks provide campsite facilities, use them for everyone´s good and for the use of toilets and showers!